-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[8.x] Fix fields not required with required_unless #37262
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
[8.x] Fix fields not required with required_unless #37262
Conversation
This fixes an issue where the `required_unless` rule does not add a "required" error when the 'other' field isn't included in input data. Take `'field' => 'required_unless:other,true'` as an example. With `[]`, the absense of `other` implies its value is *not `true`* and therefore `field` should be required. Instead, validation automatically passes. Flipping `return true` fixed the failing test but failed with valid data that *wasn't* tested. The extra two tests ensure it still passes if the field is required *and provided*, or absent when 'other' *expects* null.
@driesvints Mind checking this over please? From what I can see, the second point in #37128 should be fine for most rules, it's only the unless that has an issue. I think most null/true type cases should be covered with this, but I'd appreciate your input in case I missed something |
In the example you've given the field is not present in the data set. The If @taylorotwell we've made the adjustments under the above understanding. If it should be the other way around please let me know and I'll tackle those use cases as well from 6.x and up. |
IMO it should likely be required. It's required if |
I'll try to update the behavior on 6.x as well soon as well as for the exclude_unless rule. |
To provide the real-world example it was based on, my project was failing with the data The value of anotherfield seems to be of particular importance here. With |
@dshoreman okay, I think we finally got to the bottom of the behavior we want. I already sent in #37291 to backport it to 6.x and will send in a PR to the docs as well. |
Thanks @driesvints, much appreciated. This was the only thing left broken for me in the most recent version, so I look forward to updating from 8.37 soon! 🚀 |
In v8.40, given a form with this data:
...and this rule in the FormRequest:
The short-circuit introduced in #37128 prevents 'validateRequired' from being called on bar when baz is omitted from data - even though the very lack of it might mean "bar" should be required.
Removing the short-circuit from
required_unless
seems to solve the issue without breaking anything else. I triedfalse
first, but that just broke it differently so I added cases to cover that too.