diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index a92a19835b6c..dca3b45b47ba 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -1534,6 +1534,16 @@ public function validateProhibitedUnless($attribute, $value, $parameters) return true; } + /** + * Indicate that an attribute is excluded. + * + * @return bool + */ + public function validateExclude() + { + return false; + } + /** * Indicate that an attribute should be excluded when another attribute has a given value. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 97ba126416b6..5381ae755e4a 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -254,7 +254,7 @@ class Validator implements ValidatorContract * * @var string[] */ - protected $excludeRules = ['ExcludeIf', 'ExcludeUnless', 'ExcludeWithout']; + protected $excludeRules = ['Exclude', 'ExcludeIf', 'ExcludeUnless', 'ExcludeWithout']; /** * The size related validation rules. diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 431167e18fa6..12e66a7a967c 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -6243,6 +6243,49 @@ public function testExcludeIfWhenValidationFails($rules, $data, $expectedMessage $this->assertSame($expectedMessages, $validator->messages()->toArray()); } + public function providesPassingExcludeData() + { + return [ + [ + [ + 'has_appointment' => ['required', 'bool'], + 'appointment_date' => ['exclude'], + ], [ + 'has_appointment' => false, + 'appointment_date' => 'should be excluded', + ], [ + 'has_appointment' => false, + ], + ], + ]; + } + + /** + * @dataProvider providesPassingExcludeData + */ + public function testExclude($rules, $data, $expectedValidatedData) + { + $validator = new Validator( + $this->getIlluminateArrayTranslator(), + $data, + $rules + ); + + $passes = $validator->passes(); + + if (! $passes) { + $message = sprintf("Validation unexpectedly failed:\nRules: %s\nData: %s\nValidation error: %s", + json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), + json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), + json_encode($validator->messages()->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) + ); + } + + $this->assertTrue($passes, $message ?? ''); + + $this->assertSame($expectedValidatedData, $validator->validated()); + } + public function testExcludingArrays() { $validator = new Validator(