Fix validator treating null as true for (required|exclude)_(if|unless) due to loose in_array() check #36498
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There's currently some unusual behavior with a number of validation rules that are using
in_array()
in the default loose mode.For example, this currently fails:
This is because the
null
parameter is converted to a string'null'
and placed in an array, which is passed as the haystack toin_array()
, with the dependent field value as the needle. e.g.We can pass
true
as the third parameter to use strict mode, however many people no doubt rely on it loosely comparing numeric strings and integers, so this PR instead only enables strict mode when the value is a boolean, which fixes the bug hopefully without breaking any expected behavior.I'm not sure if this is considered a breaking change - I guess that depends on whether anyone is depending on the existing behavior. It does feel risky to tweak the behavior of such fundamental validation rules though.