Skip to content

Basic Usage

albertmoreno edited this page Aug 10, 2015 · 47 revisions

Laravel Javascript Validation ships with a simple, convenient facility for configuring rules error messages 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 rules

To can create Javascript validations using Laravel Validation Rules syntax

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

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', '#my-form')}}"></script>
 {!! $validator !!}

You can get detailed instructions how to use Laravel rules to generate Javascript validations in:

Disable Javascript validation for certain fields

Is possible disabe javascript validation for certain fields. To do that you have to add no_js_validation to fields rules.

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

In this example the field phone wont be validated with Javascript

Clone this wiki locally