Custom validation in Laravel 4

前端 未结 2 1555
遥遥无期
遥遥无期 2021-01-21 13:08

To add a new validation to Laravel I have done this:

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

and then included it in

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 13:50

    I would create custom app/validators folder for this then.

    1, Create app/validators/CustomValidate.php

    2, run php artisan optimize or composer dumpautoload

    3, Somewhere register your custom validator. Perhaps add app/validators.php into start/global.php

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

    4, Validate

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

提交回复
热议问题