E
E
E6APB2017-08-12 09:40:28
Laravel
E6APB, 2017-08-12 09:40:28

What is the meaning of bail in Laravel?

The Laravel validator has a bail rule.
Here's how it's described in the documentation:


Sometimes it is necessary to stop executing the input validation rules for an attribute after the first error. To do this, assign a bail rule to the attribute:
$this->validate($request, [
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
  ]);

If the required rule on the title attribute fails, then the unique rule will not be checked. The rules will be checked in the order they are assigned.

But without bail, the same thing is done! If a rule is not met, the next ones are not checked either. Why do you need bail?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor, 2017-08-12
@E6APB

This in the case of required is not checked if the field is not set. Otherwise, all rules are checked. bail allows you to stop at the first failure. The documentation is not a very good example, it seems.
For example, if the field is filled, then uniqueness is checked. If bail is present, then max will not be checked in case of uniqueness failure. Otherwise it will.

A
Alexander Aksentiev, 2017-08-12
@Sanasol

In order not to receive 5 errors for each field at the same time.
In terms of design, it's bad.
But in terms of usability, it infuriates when they write one error, and you don’t know what rules you still need to follow, and you send the form 50 times to go through all the errors. Here is a bail about it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question