Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After validation hook in validation request

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

like image 403
Norgul Avatar asked Dec 18 '17 09:12

Norgul


1 Answers

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;
}   
like image 159
Marcin Nabiałek Avatar answered Oct 06 '22 13:10

Marcin Nabiałek