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

osukono's avatar

Laravel validation - bail rule

Could someone please explain, why we need bail rule in validation if it doe's stop running after the first failure by default?

0 likes
5 replies
realrandyallen's avatar

It's per attribute that you're checking, you're not bailing on the entire validation process, only on other checks on that particular attribute: From the docs:

Stopping On First Validation Failure

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

In this example, if the unique rule on the title attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned.

https://laravel.com/docs/master/validation#quick-writing-the-validation-logic

1 like
osukono's avatar

But that is where the official docs are misleading. Simple check shows that it does not continue validation on an attribute after one of the rules fails. It is even obvious that we would not start testing the value on a table if it is for example fails on integer rule.

Kachelle's avatar

You can solve it by splitting the validation in two steps:

$request->validate([
    'title' => 'required|unique:posts|max:255',
]);

$request->validate([
    'title' => 'required|unique:posts|max:255', // this is not necessary
    'body' => 'required',
]);
1 like
arozhnov's avatar

It's always useful not only for topic starter but also for future generations that encounters same problems. Like me now.

Please or to participate in this conversation.