Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
[5.2] Added new validation rule to skip other rules on first validation rule fail #11986
Reference issue - #4789
New validation rule - bail (rule name updated by Taylorotwell)
When we use multiple rules, sometimes it is required to stop validating other rules on a specific rule fails.
For example, if you have an
'user_id' => 'numeric|unique:users'
In this case we can use this optional rule
'user_id' => 'bail|numeric|unique:users'
Now only if the
You have to give the rules in proper order to execute on by one.
I am sure that this can help many!
referenced this pull request
Jan 22, 2016
I have always wanted something like this in laravel. Taylor mentioned in the issue that laravel by default checks all rules because you should "show user all that's failing" -- what about those that don't want to actually show all errors. It's very common to just show the
As for the implementation in this PR, I'm not sure how I feel about adding it inside the rules, it seems ok I guess but I would also like something global for all the attributes on the validator instance. It could be annoying having to put that rule on every attribute if all you do is output first failing error message.
This could be achieved by adding a method to the validator like so:
@garygreen You are right. But what if there is only one field that you want to add this rule and for all others you need all the errors? I always wanted to get all the error messages like @taylorotwell said. But for few fields with
Like you said setting something globally for all the rules (optionally) is also a good thing. I think we may implement that separately.
@Joel-James well with the suggested
I have the following use-case scenario:
$data = [ 'module_slug' => 'home', 'type' => 'website' ]; $validator = validator($data, [ 'module_slug' => 'bail|required_if:type,module|unique:menus_items,slug' ]);
The expected result here would be to bail once required_if rule does not go through. Any ideas?