Custom Validation in Laravel 4

To add a new check in Laravel, I did the following:

created a new file called customValidation.php in app/start/

and then included it in app/start/global.php and tested it with echo() , and it worked. Now it loads into the application.

Then I wrote the following code to check for flags in Laravel:

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ class customValidate extends Illuminate\Validation\Validator { public function validateCheckbox($attribute, $value, $parameters) { //if(isset($value)) { return true; } else { return true; } echo "this is the: " . $value; } } /** * Resolvers for Custom Validations */ Validator::resolver(function($translator, $data, $rules, $message){ return new customValidate($translator, $data, $rules, $message); }); 

However, in my validation rules, I define:

 `array('sex'=>'checkbox')` 

But that will not work. Nothing has happened. An error does not occur. The application behaves as if it did not perform this function at all. Also, when I repeat something from within the function, nothing gets echoed, another proof that the function is not called at all.

+2
source share
2 answers

When the form was submitted, the radio stations were not installed in their initial states, and this caused Laravel not to search for the item validator. So there is no workaround for this. What I did, I just added the initial state to the radios, and they worked.

Much as Andreiko said

0
source

I would create a custom app/validators for this.

1, Create app/validators/CustomValidate.php

 <?php class CustomValidate extends Illuminate\Validation\Validator { public function validateCheckbox($attribute, $value, $parameters) { echo "this is the: " . $value; } } 

2, do php artisan optimize or composer dumpautoload

3, Somewhere register your own validator. Maybe add app/validators.php to start/global.php

 Validator::resolver(function($translator, $data, $rules, $message){ return new CustomValidate($translator, $data, $rules, $message); }); 

4, confirm

 $rules = ['agreed' => 'checkbox']; $data = ['agreed' => 0]; $v = Validator::make($data, $rules); dd($v->passes()); 
+5
source

Source: https://habr.com/ru/post/1245919/


All Articles