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