Search code examples
phpvalidationlaravellaravel-4short-circuiting

How validation rules work in laravel


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?


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.