Question

In laravel, in order to validate some input from user, we can use Validator Class.

for example for a registration by email , validation rule can be:

array( 'email' => 'required|email|unique:users,email' )

Which says, email is required, email should be in email format, and should not registered in table users before. ( should be unique )

So, How does this work?

Does it use short-circuit ? If we go through step by step

  • it checks if input is set by required
    if Passed, jumps to next rule
  • then checks if it is in email format
    if Passed, jumps to next rule
  • checks if not exists in table users

I asked someone and he said , it check all , goes through all rules.
If required rule is not passed, there is no reason to check if input is in email format.
And if it is not in email format, there is no need to check database.

Does anyone know how it work?

Was it helpful?

Solution

This depends on the rule. In practice, Laravel will stop processing other rules if the required attribute is failed. However, if required passes, it will continue to validate other rules.

This means you can receive multiple Validation Errors on the same field.

OTHER TIPS

I could not find details for Laravel 4, but the Laravel 5.4 documentation implies that all validation rules are run in order without short-circuit evaluation.

In Laravel 5, you can force short-circuit evaluation by prefixing 'bail' to the validation rule.

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

I came here looking for why short-circuiting would be off by default, I think James is correct in his reasoning that it is for receiving multiple validation errors on the same field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top