New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.2] Added new validation rule to skip other rules on first validation rule fail #11986
Conversation
@@ -316,6 +316,11 @@ public function passes() | |||
foreach ($this->rules as $attribute => $rules) { | |||
foreach ($rules as $rule) { | |||
$this->validate($attribute, $rule); | |||
|
|||
// Break validation on first fail if failonfirst is set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this comment.
Please squash to one commit. |
@GrahamCampbell Thank you. It's done. |
} | ||
} | ||
|
||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Joel-James, why not just
return $this->hasRule($attribute, ['Failonfirst']) && $this->messages->has($attribute);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucasmichot That is good but I think the previous one is fast and easily readable? Should I change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather:
if (! $this->hasRule($attribute, ['Failonfirst'])) {
return false;
}
return $this->messages->has($attribute);
Just my feeling, at the end @taylorotwell can always tweak it.
Wouldn't it make more sense for Laravel to automatically check if the unique check would cause any database errors? |
@CupOfTea696 For database unique queries that may work. But for others? |
/** | ||
* Stop on error if failonfirst rule is given. | ||
* | ||
* First check if 'failonfirst' is set, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too short.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Infact, this description is pretty pointless and should just be deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by 'line is too short'? Which line?
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 |
@Joel-James is there any workaround where bail would work together with a required_if ? #12500 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? |
@codewizz did you found any solutions?? I am stuck on same issue |
@neophyt3 Your |
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
int
field in database and you are usingnumeric
rule andunique
rule for the field. This may throw a Database error if the input is not numeric. Because validation checks both the rules.Numeric check will return false, but exists check will also execute with non-integer value.
In this case we can use this optional rule
bail
to execute validation in rule order and skip other rules if any of the rules fails. For example,Now only if the
numeric
rule pass, validation will checkunique
rule, that will avoid db error.This rule can be optional. Validation will be skipped only if we use this rule.
You have to give the rules in proper order to execute on by one.
I am sure that this can help many!