Feb 4, 2024
3
Level 1
How to use FormRequest::after() in Laravel10.x?
The after() method of the FormRequest class, which appears to have been implemented since Laravel 10.x, is not executed when actually defined in a FormRequest.
use Illuminate\Validation\Validator;
/**
* Get the "after" validation callables for the request.
*/
public function after(): array
{
return [
function (Validator $validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add(
'field',
'Something is wrong with this field!'
);
}
}
];
}
This is because the withValidator() method, which appears to have once been called in the getValidatorInstance() method, is still there, and the after(), which appears to be the new replacement, is not called from anywhere.
protected function getValidatorInstance()
{
if ($this->validator) {
return $this->validator;
}
$factory = $this->container->make(ValidationFactory::class);
if (method_exists($this, 'validator')) {
$validator = $this->container->call([$this, 'validator'], compact('factory'));
} else {
$validator = $this->createDefaultValidator($factory);
}
if (method_exists($this, 'withValidator')) {
$this->withValidator($validator);
}
$this->setValidator($validator);
return $this->validator;
}
Do I have to do anything additional?
By the way, the version of Laravel is 10.3.2.
Thank you.
Please or to participate in this conversation.