验证请求中的验证挂钩后
After validation hook in validation request
我可以使用 php artisan make:request
将验证挂钩 (documentation) 附加到我的自定义请求吗?
您可以在自定义请求 class 中覆盖 getValidatorInstance()
方法,如下所示:
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
// here you can apply hook (example hook taken from documentation):
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
return $validator;
}
我可以使用 php artisan make:request
将验证挂钩 (documentation) 附加到我的自定义请求吗?
您可以在自定义请求 class 中覆盖 getValidatorInstance()
方法,如下所示:
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
// here you can apply hook (example hook taken from documentation):
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
return $validator;
}