Laravel:调用fail()或pass()方法后,无法向验证程序添加自定义错误

时间:2017-01-25 08:12:18

标签: php laravel laravel-5.3 laravel-validation

fails()上调用passes()Validator方法后,我无法使用error validator添加after } hook

$validator = Validator::make(
    ['name' => 'kanav'],
    ['name' => 'required']
);

if($validator->fails()){...} // I've some code around this

if($somecondition) {
    $validator->after(function ($validator) {
        $validator->errors()->add('name', 'Name Invalid');
    });
}

dd($validator->errors()->first()); //returns ""
// dd($validator->fails()); // returns true

1 个答案:

答案 0 :(得分:0)

doc开始,尝试在调用失败之前声明后挂钩,就像这样

$validator = Validator::make(
    ['name' => 'kanav'],
    ['name' => 'required']
);

if($somecondition) {
    $validator->after(function ($validator) {
        $validator->errors()->add('name', 'Name Invalid');
    });
}

if($validator->fails()){...}