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 exclude validation rules for nested data #31006

Merged
merged 4 commits into from Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Validation/Validator.php
Expand Up @@ -182,6 +182,7 @@ class Validator implements ValidatorContract
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
'ExcludeIf', 'ExcludeUnless',
];

/**
Expand Down
107 changes: 107 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -4910,6 +4910,70 @@ public function providesPassingExcludeIfData()
'has_appointments' => false,
],
],
'nested-05' => [
[
'vehicles.*.type' => 'required|in:car,boat',
'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
], [
'vehicles' => [
['type' => 'car', 'wheels' => 4],
['type' => 'boat', 'wheels' => 'should be excluded'],
],
], [
'vehicles' => [
['type' => 'car', 'wheels' => 4],
['type' => 'boat'],
],
],
],
'nested-06' => [
[
'vehicles.*.type' => 'required|in:car,boat',
'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
], [
'vehicles' => [
['type' => 'car', 'wheels' => 4],
['type' => 'boat'],
],
], [
'vehicles' => [
['type' => 'car', 'wheels' => 4],
['type' => 'boat'],
],
],
],
'nested-07' => [
[
'vehicles.*.type' => 'required|in:car,boat',
'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
'vehicles.*.wheels.*.color' => 'required|in:red,blue',
// In this bizzaro world example you can choose a custom shape for your wheels if they are red
'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
], [
'vehicles' => [
['type' => 'car', 'wheels' => [
['color' => 'red', 'shape' => 'square'],
['color' => 'blue', 'shape' => 'hexagon'],
['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
['color' => 'blue', 'shape' => 'triangle'],
]],
['type' => 'boat'],
],
], [
'vehicles' => [
['type' => 'car', 'wheels' => [
// The shape field for these blue wheels were correctly excluded (if they weren't, they would
// fail the validation). They still appear in the validated data. This behaviour is unrelated
// to the "exclude" type rules.
['color' => 'red', 'shape' => 'square'],
['color' => 'blue', 'shape' => 'hexagon'],
['color' => 'red', 'shape' => 'round', 'junk' => 'no rule, still present'],
['color' => 'blue', 'shape' => 'triangle'],
]],
['type' => 'boat'],
],
],
],
];
}

Expand Down Expand Up @@ -4980,6 +5044,49 @@ public function providesFailingExcludeIfData()
'appointments.1.name' => ['validation.required'],
],
],
[
[
'vehicles.*.price' => 'required|numeric',
'vehicles.*.type' => 'required|in:car,boat',
'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|numeric',
], [
'vehicles' => [
[
'price' => 100,
'type' => 'car',
],
[
'price' => 500,
'type' => 'boat',
],
],
], [
'vehicles.0.wheels' => ['validation.required'],
// vehicles.1.wheels is not required, because type is not "car"
],
],
'exclude-validation-error-01' => [
[
'vehicles.*.type' => 'required|in:car,boat',
'vehicles.*.wheels' => 'exclude_if:vehicles.*.type,boat|required|array',
'vehicles.*.wheels.*.color' => 'required|in:red,blue',
// In this bizzaro world example you can choose a custom shape for your wheels if they are red
'vehicles.*.wheels.*.shape' => 'exclude_unless:vehicles.*.wheels.*.color,red|required|in:square,round',
], [
'vehicles' => [
['type' => 'car', 'wheels' => [
['color' => 'red', 'shape' => 'square'],
['color' => 'blue', 'shape' => 'hexagon'],
['color' => 'red', 'shape' => 'hexagon'],
['color' => 'blue', 'shape' => 'triangle'],
]],
['type' => 'boat', 'wheels' => 'should be excluded'],
],
], [
// The blue wheels are excluded and are therefor not validated against the "in:square,round" rule
'vehicles.0.wheels.2.shape' => ['validation.in'],
],
],
];
}

Expand Down