#Are FormRequest::after() callables always run?

2 messages · Page 1 of 1 (latest)

frail hamlet
#

I'm running into some issues with validating a FormRequest. I have a rule that checks for the existence of a particular field:

public function rules(): array
    {
        return [
            'code' => 'bail|required|string|exists:promotions',
        ];
    }

I then have several other validations in the public function after() array of callables.
Unfortunately it seems like bail isn't working, because when I enter a non-existent value, one of the after() callables errors out because it can't find a promotion with the given code.
Am I misunderstanding how rules and after are evaluated?

median shell
#

Well, the hook runs after validation, the code field is validated, from the docs:

The after method should return an array of callables or closures which will be invoked after validation is complete. [...] allowing you to raise additional error messages if necessary
So yes, my understanding from the docs; the hook will run after validation, that means it will run upon successful validation, but also when failures are encountered. So you code should account for that.