After validation hook in validation request
Asked Answered
W

1

7

Can I attach after validation hook (documentation) to my custom made request with php artisan make:request?

Wellborn answered 18/12, 2017 at 9:36 Comment(0)
B
11

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;
}   
Botvinnik answered 18/12, 2017 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.