Skip to content

Controller validation

Paulo Silva edited this page Sep 22, 2016 · 2 revisions

Class and rule syntax are the same that Laravel Validation class implements.

 $validator = JsValidator::make(
     [
        'name' => 'required',
        'password' => 'required|min:8',
        'email' => 'required|email|unique:users'
      ],
      [
        'name' => 'validation.my_custom_lang_message',
        'password.required' => ':attribute can\'t be empty',
        'password.min' => ':attribute should has at least :min chars',
      ],
      [
        'name' => 'Username',
        'password' => 'Password'
        'email' => 'E-Mail'
       ],
       '#myform'
 );

First argument passed to the make method is the validation rules that should be applied to the data. The rule syntax is the same that Laravel Validation class.

Second (optional) argument passed to the make method is the custom error messages showed when validation fails. By default jsValidator uses the Laravel messages translated according [Laravel Localization][] settings but if you want you can customize the message for each rule.

**Third (optional) argument** passed to the make` method is the custom attribute name displayed in error messages.

**Fourth (optional) argument** passed to the makemethod is the JQuery selector to find the form to validate. By default this selector isformso, any form will be validated. You can change the default selector inconfig/jsvalidation.php` config file