From 5140d15d73f6f573a2e56b96cbdf77425d4e6713 Mon Sep 17 00:00:00 2001 From: Ahmed Badawy Date: Wed, 20 Dec 2023 14:54:53 +0200 Subject: [PATCH 1/2] support missing rule with nested array data --- src/Illuminate/Validation/Validator.php | 4 ++++ tests/Validation/ValidationValidatorTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 3894e6e141ba..35d18149af79 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -265,6 +265,10 @@ class Validator implements ValidatorContract 'ProhibitedIf', 'ProhibitedUnless', 'Prohibits', + 'MissingIf', + 'MissingUnless', + 'MissingWith', + 'MissingWithAll', 'Same', 'Unique', ]; diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 6ce65cc2b6d5..006230684ce3 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2498,6 +2498,10 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'bar' => '2'], ['foo' => 'missing_if:bar,1']); $this->assertTrue($v->passes()); + + $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_if:foo.*.bar,1']); + $this->assertTrue($v->fails()); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar is 1.',$v->errors()->first('foo.0.baz')); } public function testValidateMissingUnless() @@ -2537,6 +2541,10 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'bar' => '1'], ['foo' => 'missing_unless:bar,1']); $this->assertTrue($v->passes()); + + $v = new Validator($trans,['foo' => [0 => ['bar' => 0, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_unless:foo.*.bar,1']); + $this->assertTrue($v->fails()); + $this->assertSame('The foo.0.baz field must be missing unless foo.0.bar is 1.',$v->errors()->first('foo.0.baz')); } public function testValidateMissingWith() @@ -2579,6 +2587,10 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'qux' => '1'], ['foo' => 'missing_with:baz,bar']); $this->assertTrue($v->passes()); + + $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_with:foo.*.bar,foo.*.fred']); + $this->assertTrue($v->fails()); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred is present.',$v->errors()->first('foo.0.baz')); } public function testValidateMissingWithAll() @@ -2621,6 +2633,10 @@ public function count(): int $v = new Validator($trans, ['foo' => [], 'bar' => '2', 'qux' => '2'], ['foo' => 'missing_with_all:baz,bar']); $this->assertTrue($v->passes()); + + $v = new Validator($trans,['foo' => [0 => ['bar' => 1,'fred' => 2, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_with_all:foo.*.bar,foo.*.fred']); + $this->assertTrue($v->fails()); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred are present.',$v->errors()->first('foo.0.baz')); } public function testValidateDeclinedIf() From efdba4a1c1e432c9a2f642f1461db7477868921f Mon Sep 17 00:00:00 2001 From: Ahmed Badawy Date: Wed, 20 Dec 2023 15:33:58 +0200 Subject: [PATCH 2/2] fix style --- tests/Validation/ValidationValidatorTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 006230684ce3..ce48adf6c196 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2499,9 +2499,9 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'bar' => '2'], ['foo' => 'missing_if:bar,1']); $this->assertTrue($v->passes()); - $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_if:foo.*.bar,1']); + $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]], ['foo.*.baz' => 'missing_if:foo.*.bar,1']); $this->assertTrue($v->fails()); - $this->assertSame('The foo.0.baz field must be missing when foo.0.bar is 1.',$v->errors()->first('foo.0.baz')); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar is 1.', $v->errors()->first('foo.0.baz')); } public function testValidateMissingUnless() @@ -2542,9 +2542,9 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'bar' => '1'], ['foo' => 'missing_unless:bar,1']); $this->assertTrue($v->passes()); - $v = new Validator($trans,['foo' => [0 => ['bar' => 0, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_unless:foo.*.bar,1']); + $v = new Validator($trans,['foo' => [0 => ['bar' => 0, 'baz' => 'should be missing']]], ['foo.*.baz' => 'missing_unless:foo.*.bar,1']); $this->assertTrue($v->fails()); - $this->assertSame('The foo.0.baz field must be missing unless foo.0.bar is 1.',$v->errors()->first('foo.0.baz')); + $this->assertSame('The foo.0.baz field must be missing unless foo.0.bar is 1.', $v->errors()->first('foo.0.baz')); } public function testValidateMissingWith() @@ -2588,9 +2588,9 @@ public function count(): int $v = new Validator($trans, ['foo' => 'foo', 'qux' => '1'], ['foo' => 'missing_with:baz,bar']); $this->assertTrue($v->passes()); - $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_with:foo.*.bar,foo.*.fred']); + $v = new Validator($trans,['foo' => [0 => ['bar' => 1, 'baz' => 'should be missing']]], ['foo.*.baz' => 'missing_with:foo.*.bar,foo.*.fred']); $this->assertTrue($v->fails()); - $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred is present.',$v->errors()->first('foo.0.baz')); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred is present.', $v->errors()->first('foo.0.baz')); } public function testValidateMissingWithAll() @@ -2634,9 +2634,9 @@ public function count(): int $v = new Validator($trans, ['foo' => [], 'bar' => '2', 'qux' => '2'], ['foo' => 'missing_with_all:baz,bar']); $this->assertTrue($v->passes()); - $v = new Validator($trans,['foo' => [0 => ['bar' => 1,'fred' => 2, 'baz' => 'should be missing']]],['foo.*.baz' => 'missing_with_all:foo.*.bar,foo.*.fred']); + $v = new Validator($trans,['foo' => [0 => ['bar' => 1,'fred' => 2, 'baz' => 'should be missing']]], ['foo.*.baz' => 'missing_with_all:foo.*.bar,foo.*.fred']); $this->assertTrue($v->fails()); - $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred are present.',$v->errors()->first('foo.0.baz')); + $this->assertSame('The foo.0.baz field must be missing when foo.0.bar / foo.0.fred are present.', $v->errors()->first('foo.0.baz')); } public function testValidateDeclinedIf()