Skip to content

Basic Usage

Omar Bahareth edited this page Jan 8, 2019 · 47 revisions

Laravel Javascript Validation ships with a simple, convenient facility for configuring rules, messages and attributes via the JsValidator Facade.

When JsValidator instance is printed in a view, the Javascript code needed to validate your form is rendered to the page.

Create Javascript validations using Laravel Form Request

The easiest way to create Javascript validations is using Laravel Form Request Validation.

You can call the JsValidator::formRequest() from your view passing your FormRequest to be validated. The javascript code will be generated using the configured template.

 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>

 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ url('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! JsValidator::formRequest('App\Http\Requests\MyFormRequest') !!}

You can get more information how to use FormRequests to create Javascript validations

Create Javascript validations using rules

You can create Javascript validations using Laravel Validation Rules syntax

$validator = JsValidator::make(['name_field'=>'required']);

and render the object in your view.

 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>

 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ asset('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! $validator->selector('#my-form') !!}

You can get more information of how to use Laravel rules to generate Javascript validations:

Create Javascript validations using Validator instance

You can create Javascript validations using existing Laravel Validator instance

$validator = Validator::make(['name_field'=>'required']);
$jsValidator = JsValidator::validator($validator)

and render the object in your view.

 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>

 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ asset('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! $jsValidator->selector('#my-form') !!}

You can get more information of how to use Laravel rules to generate Javascript validations:

Complex Conditional Validation

For Contitionally addiding rules you must indicate to JsValidation object witch attribute and rules will be validated conditionally. The $callback function is not needed because is evaluated using AJAX.

$js = JsValidator::make($rules);
$js->sometimes('reason', 'required|max:500');

You can get more information of how to use Complex Conditional Validation

Validating multiple forms

You can validate multiple forms specifying the jQuery seletor where validations will be applied

$validatorUserForm = JsValidator::make(['username'=>'required']);
$validatorSearchForm = JsValidator::make(['search'=>'min:3']);

and render the object in your view. The javascript code will be generated using the configured template.

 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>

 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ asset('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! $validatorUserForm->selector('#user-form') !!}
 {!! $validatorSearchForm->selector('#search-form') !!}

You can get more information in JsValidator render reference

Disable Javascript validation for certain fields

Is possible disable javascript validation for certain fields. To do that you have to add no_js_validation to fields rules. In this example the field phone wont be validated with Javascript

$rules=array(
     'name'  =>'required',
     'phone' => 'numeric|no_js_validation'
);

You can also disable Ajax validations. to do that modify config/jsvalidation.php file

    /*
     * Enable or disable Ajax validations of Database and custom rules.
     * By default Unique, ActiveURL, Exists and custom validations are validated via AJAX
     */
    'disable_remote_validation' => true,
Clone this wiki locally