Skip to content
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

[6.x] Fix required_if boolean validation #36969

Merged

Conversation

driesvints
Copy link
Member

Re-send of #36967

This should fix some regressions for the changed behavior introduced in #36504. The PR now takes into account the boolean validation rule when combining rules like required_if. As reported in #36952 atm the validator will incorrectly return incorrect error messages when a boolean value of 0 is used. By first checking if the rules for the subject of required_if contains boolean we can safely know that it should be converted to a boolean. This doesn't breaks previous tests and all tests introduced by @jessarcher still pass.

I'd appreciate a second pair of eyes on this @jessarcher @timacdonald @SjorsO @taylorotwell.

Fixes #36952

Copy link
Member

@jessarcher jessarcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Sorry for the headache!

@driesvints
Copy link
Member Author

Thanks @jessarcher. Don't worry about it 🙂

@driesvints driesvints requested a review from taylorotwell April 14, 2021 07:01
@taylorotwell taylorotwell merged commit 15b6358 into 6.x Apr 14, 2021
@taylorotwell taylorotwell deleted the revert-36968-revert-36967-fix-required-if-validation branch April 14, 2021 11:58
@aromka
Copy link

aromka commented Apr 24, 2021

This now breaks nullable values:

'foo' => 'nullable|boolean',
'faz' => 'nullable|required_if:foo,false'

Previously if foo was missing from the request - it was ignoring this validation, now it treats null as false and fails the validation.
Setting 'faz' => 'nullable|required_unless:foo,true,null' also doesn't work and still fails validation.

(Using Laravel 8.x)

From documentation:

...you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid

This is no longer correct since validator considers null values as false making the validation fail on an optional field.

@driesvints
Copy link
Member Author

@aromka I'm looking into this now.

@driesvints
Copy link
Member Author

@aromka PR made here: #37128

@driesvints
Copy link
Member Author

@aromka would appreciate your review on that one.

@oprypkhantc
Copy link
Contributor

@driesvints This is still a breaking change. Every piece of validation that used 0/1 instead of false/true in required_if just stopped working as expected.

@uuf6429
Copy link

uuf6429 commented Aug 2, 2021

I don't like posting "me too" replies, but I agree with @oprypkhantc, we also have this problem and it's fixed by changing 1 to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants