验证请求后验证钩子

时间:2017-12-18 09:36:04

标签: php laravel laravel-5 laravel-validation laravel-request

我可以在验证挂钩(documentation)后附加到php artisan make:request的自定义请求吗?

1 个答案:

答案 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;
}