Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tkydev's avatar

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.

0 likes
3 replies
tkydev's avatar

@snapey Thanks to you I have discovered that it is a minor version of the problem.

Looking at the release version of Laravel, the after method seemed to be from 10.8.0.

Thank so much.

Artwork's avatar

@tkydev, wonderful day! Though, the actual usage is not stated, have you considered marking the question as solved knowing the question age?

Please or to participate in this conversation.