Laravel 5 FormRequest Validation with JavaScript

Laravel Javascript Validation reuses your Laravel FormRequest Validators to validate client-side forms automatically without writing any Javascript.

Laravel Javascript Validation - TutorialLaravel

Today I am going to show you how you can handle your FormRequest class validation into Javascript without refreshing any page.

I am going to use Laravel 5.4 Here. Laravel-jsvalidation package provides a feature to handle this thing easily. I am going to show you simple demo example where I will use my Request validation class into JavaScript.

Before moving on the code it’s good to know dependency of code. This package uses jquery validate plugin and twitter bootstrap. of course, You can change it as per your own way.

So let’s begin. You need to install package or clone it from Github. I would like to use composer command.

After successful installation, you need to register service provider and facade into your app.php file into config folder.

Also, don’t forget to publish configurations file of this package by using this command.

The above command will create jsvalidation.php into your config folder and jsvalidation into public/vendor directory and it will also create jsvalidation folder at resources/views/vendor.

You can see bootstrap.php into resources/views/vendor/jsvalidation directory. This is a template that provided by default for bootstrap error stack. You can change it and also you can write it as per your framework like Bulma or Materialcss. Of course, you need to change ‘view’ parameter at config file of jsvalidation.

I am here going to use bootstrap simple form and Laravel Form Request class for my validation. So here is my routing file which is web.php

Here is my RegisterController.php file which is inside app/Http/Controllers/RegisterController.php

As you can see getRegister method display page where actual form appearance. The Same postRequest method has an injection of FormRequest class where my validation rules are exits. Here is that:

rules() contains rules for my form. There are three different form inputs. Simple rules are all fields are required.

Now important thing in view file you need to add vendor/jsvalidation/js/jsvalidation.js as a script file. That file contains actual JavaScript validation code. In my case my view file is welcome.blade.php

In my Form file, there is three input and form tag with the csrf_token field. Because it’s required with every POST request either Laravel will give you TokenMismatch error.

Here I used bootstrap CDN. And I also added jsvalidation javascript file. Then using JsValidator facade I write my FormRequest rules. JsValidator class Contains formRequest method in which my first argument is FormRequest class and second is my form tag selector.

Now JavaScript will validate your form without refreshing page. of course, you can use any validation rule. It supports all


Leave a Reply

Your email address will not be published. Required fields are marked *