-
-
Notifications
You must be signed in to change notification settings - Fork 175
Basic Usage
[Create Javascript validations using Laravel Form Request](#Create Javascript validations using Laravel Form Request)
[Create Javascript validations using rules](#Create Javascript validations using rules)
[Create Javascript validations using Validator instance](Create Javascript validations using Validator instance) [Validating multiple forms](Validating multiple forms)
[Disable Javascript validation for certain fields](Disable Javascript validation for certain fields)
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.
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
- [Form Request] (Form-Requests)
- JsValidator::formRequest() reference
- [FormRequest Example](FormRequest Validation Example)
You can create Javascript validations using Laravel Validation Rules syntax
$validator = JsValidation::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', '#my-form')}}"></script>
{!! $validator !!}
You can get bor information of how to use Laravel rules to generate Javascript validations:
- Using Controller validation
- JsValidator::make() reference
- [Controller Validation Example](Controller Validation Example)
You can create Javascript validations using existing Laravel Validator instance
$validator = Validator::make(['name_field'=>'required']);
$jsValidator = JsValidation::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', '#my-form')}}"></script>
{!! $jsValidator !!}
You can get bor information of how to use Laravel rules to generate Javascript validations:
- Using Controller validation
- JsValidator::validator() reference
- [Controller Validation Example](Controller Validation Example)
You can validate multiple forms specifying the jQuery seletor where validations will be applied
$validatorUserForm = JsValidation::make(['username'=>'required']);
$validatorSearchForm = JsValidation::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', '#my-form')}}"></script>
{!! $validatorUserForm->selector('#user-form') !!}
{!! $validatorSearchForm->selector('#search-form') !!}
You can get more information in JsValidator render reference
Is possible disable 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