我可以在验证挂钩(documentation)后附加到php artisan make:request
的自定义请求吗?
答案 0 :(得分:7)
您可以在自定义请求类中覆盖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;
}