在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
答案 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()){...}