验证请求后验证钩子

问题描述 投票:2回答:1

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

php laravel laravel-5 laravel-validation laravel-request
1个回答
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;
}   
© www.soinside.com 2019 - 2024. All rights reserved.