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

[5.7] Improve nested rules in validated data #23708

Merged
merged 1 commit into from
Mar 27, 2018
Merged

[5.7] Improve nested rules in validated data #23708

merged 1 commit into from
Mar 27, 2018

Conversation

staudenmeir
Copy link
Contributor

(re-submit of #23703 for 5.7)

#20974 fixed an issue with array rules (users.*.name) when returning the validated data.
Nested rules now get reduced to the first key.

However, this wouldn't be necessary for rules without an asterisk:

$data = Validator::make(['a' => ['b' => 'x', 'c' => 'y']], ['a.b' => 'required'])->validate();
// Expected: ['a' => ['b' => 'x']]
// Actual: ['a' => ['b' => 'x', 'c' => 'y']]

This PR differentiates between rules having and not having an asterisk. Only the former get reduced.

For the request validation this is easy to achieve because Request::only() already supports nested keys.

Validator::validate() however uses Collection::only() which doesn't support nested keys:

$data = collect(['a' => ['b' => 'x']])->only('a.b')->all();
// Expected: ['a' => ['b' => 'x']]
// Actual: []

So we have to use data_get() and Arr::set() (as used in Request::only()) to bring support for nested keys to Validator.

Fixes #23612.

@morloderex
Copy link
Contributor

Isn't it better to just add support for nested keys inside the collection?

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.

4 participants