diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index f6b2963b3427..0f6f0e2e1a88 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -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', ]; /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index c4de1bf2fc64..1457857560f5 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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'], + ], + ], + ], ]; } @@ -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'], + ], + ], ]; }