Skip to content

Commit

Permalink
Merge pull request #31006 from SjorsO/tests-for-validation-nesting
Browse files Browse the repository at this point in the history
[6.x] Fix exclude validation rules for nested data
  • Loading branch information
taylorotwell committed Jan 3, 2020
2 parents 1ebcf59 + 0ae3871 commit 523c0fb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
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

0 comments on commit 523c0fb

Please sign in to comment.