7

我可以在验证钩子(文档)之后附加到我的定制请求php artisan make:request吗?

4

1 回答 1

11

您可以像这样覆盖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;
}   
于 2017-12-18T09:41:05.177 回答