Custom validation in Laravel 4

前端 未结 2 1556
遥遥无期
遥遥无期 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

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

    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());
    
    0 讨论(0)
  • 2021-01-21 14:01

    When submitting the form, the radios were not set in their initial states, and this made Laravel not to look for the validator of the item. So, it doesn't have any workaround for that. What I did was I simply added an initial state to the radios and they worked.

    Much Like said by Andreyco

    0 讨论(0)
提交回复
热议问题