[8.x] Fix bug discarding input fields with empty validation rules #38563
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.
This PR fixes a breaking change that was introduced with conditional validation rules in v8.55.0.
Before the change in v8.55, if input fields were being validated with an empty list of validation rules, no validation would be performed but those fields would be included in the data returned from
$request->validated()
.Now, fields with an empty list of validation rules (
[]
or''
) are removed from the list of rules entirely, so matching input fields in the request are not included in$request->validated()
.v8.54 and earlier:
v8.55 and later:
Background
The conditional validation rules feature added in v8.55.0 runs
filter()
on the collection of validation rules so that conditional rules are not applied if the conditional is false.This unintentionally introduced the side-effect that input data is excluded from the output of
validated()
for non-conditional rules with empty ([]
or''
) rules, and for conditional rules where the conditional is false. In the second case, if the only rule on a field is a conditional rule and the condition evaluates to be false, instead of just not running the conditional rules the field will be removed fromvalidated()
completely.I'm not suggesting in this PR that the previous behaviour is or isn't correct (I asked some colleagues about this and most actually agree that the new behaviour is better), but it seems like a breaking change, so I think it would be ideal to avoid making it in a minor release. At Tighten we've already seen multiple applications break as a result of this change.
The code change in this PR is very small and completely backwards-compatible—it just stops filtering out validation rules that are
[]
or''
. If filtering these fields out is the preferred behaviour, it could easily be reintroduced in v9.Fixes #38505.