简体   繁体   中英

After validation hook in validation request

我可以使用php artisan make:request将验证钩子( 文档 )附加到我的自定义php artisan make:request吗?

You can override getValidatorInstance() method in your custom request class like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM