DaemonKeen's avatar

DaemonKeen wrote a reply+100 XP

5mos ago

Can validation be stopped on specific failures?

I've came across the same issue and solved it by creating a custom validation rule

that I've attached to the upcoming field with the field we want to stop on as parameter. Now when validator encounter the StopOnFailure it will look into the error bag for the 'driver' and throw exception.

return [
            'driver' => ['required', Rule::enum(ServiceDriver::class)],
            'name' => [new StopOnFailure('driver'), 'required', 'string', 'max:255']
];

I think this solution works but it isn't optimal. It bypasses Laravel's standard error collection and jumps straight to the response which can lead to issues.