From 46c964cd6f47d6d90867629dda0d5e5d8cb40a14 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 30 Jun 2023 17:25:58 +0200 Subject: [PATCH 1/7] fix validation of attributes that depend on others with exclude rule applied --- src/Illuminate/Validation/Validator.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 9cae4764343f..9e547864fffa 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -430,8 +430,6 @@ public function passes() $this->validateAttribute($attribute, $rule); if ($this->shouldBeExcluded($attribute)) { - $this->removeAttribute($attribute); - break; } @@ -440,6 +438,12 @@ public function passes() } } } + // Only remove attributes after having looped through all the rules, so that + // the order of the attributes with applied "exclude" rule doesn't matter. + foreach ($this->excludeAttributes as $attribute) { + $this->removeAttribute($attribute); + } + // Here we will spin through all of the "after" hooks on this validator and // fire them off. This gives the callbacks a chance to perform all kinds From d3e169e93aef39cbe64e20f0f6b364275970babe Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 30 Jun 2023 17:47:16 +0200 Subject: [PATCH 2/7] fixed style --- src/Illuminate/Validation/Validator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 9e547864fffa..fbb8f679888a 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -444,7 +444,6 @@ public function passes() $this->removeAttribute($attribute); } - // Here we will spin through all of the "after" hooks on this validator and // fire them off. This gives the callbacks a chance to perform all kinds // of other validation that needs to get wrapped up in this operation. From 934a36c5f117aa9311a786fdaa13ab681351f231 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 19 Aug 2023 20:55:29 +0330 Subject: [PATCH 3/7] Validator's passes methods fixed; --- src/Illuminate/Validation/Validator.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index fbb8f679888a..c7b38ec44d20 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -430,6 +430,7 @@ public function passes() $this->validateAttribute($attribute, $rule); if ($this->shouldBeExcluded($attribute)) { + break; } @@ -440,8 +441,10 @@ public function passes() } // Only remove attributes after having looped through all the rules, so that // the order of the attributes with applied "exclude" rule doesn't matter. - foreach ($this->excludeAttributes as $attribute) { - $this->removeAttribute($attribute); + foreach ( $this->rules as $attribute => $rules ) { + if ($this->shouldBeExcluded($attribute)) { + $this->removeAttribute($attribute); + } } // Here we will spin through all of the "after" hooks on this validator and From bb1d2e65ad66bd98be7eb7c060914b93d14daf5f Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 19 Aug 2023 20:55:57 +0330 Subject: [PATCH 4/7] tests added for passes method; --- tests/Validation/ValidationValidatorTest.php | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 3153de27e2e1..58a3e07b1cb5 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -8199,6 +8199,39 @@ public function testExclude($rules, $data, $expectedValidatedData) $this->assertSame($expectedValidatedData, $validator->validated()); } + public function testExcludeBeforeRequiredIf( ) + { + $validator = new Validator( + $this->getIlluminateArrayTranslator(), + [ + 'profile_id' => null, + 'type' => 'denied', + ], + [ + 'type' => ['required', 'string', 'exclude'], + 'profile_id' => ['nullable', 'required_if:type,profile', 'integer'], + ], + ); + + $this->assertTrue($validator->passes()); + $this->assertSame(['profile_id' => null], $validator->validated()); + + $validator = new Validator( + $this->getIlluminateArrayTranslator(), + [ + 'profile_id' => null, + 'type' => 'profile', + ], + [ + 'type' => ['required', 'string', 'exclude'], + 'profile_id' => ['nullable', 'required_if:type,profile', 'integer'], + ], + ); + + $this->assertFalse($validator->passes()); + $this->assertSame(['profile_id' => ['validation.required_if']], $validator->getMessageBag()->getMessages()); + } + public function testExcludingArrays() { $validator = new Validator( From 1705b12c29a939c5f3fe4c873c506d1ab579ad1c Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 19 Aug 2023 22:38:07 +0330 Subject: [PATCH 5/7] Apply fixes from StyleCI; --- src/Illuminate/Validation/Validator.php | 3 +-- tests/Validation/ValidationValidatorTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index c7b38ec44d20..f68a3e9d6cb5 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -430,7 +430,6 @@ public function passes() $this->validateAttribute($attribute, $rule); if ($this->shouldBeExcluded($attribute)) { - break; } @@ -441,7 +440,7 @@ public function passes() } // Only remove attributes after having looped through all the rules, so that // the order of the attributes with applied "exclude" rule doesn't matter. - foreach ( $this->rules as $attribute => $rules ) { + foreach ($this->rules as $attribute => $rules) { if ($this->shouldBeExcluded($attribute)) { $this->removeAttribute($attribute); } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 58a3e07b1cb5..e285f8268289 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -8199,7 +8199,7 @@ public function testExclude($rules, $data, $expectedValidatedData) $this->assertSame($expectedValidatedData, $validator->validated()); } - public function testExcludeBeforeRequiredIf( ) + public function testExcludeBeforeRequiredIf() { $validator = new Validator( $this->getIlluminateArrayTranslator(), From db1b149d9931f51930cd8f2d7d815f4e60eb741a Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Mon, 21 Aug 2023 09:52:46 +0330 Subject: [PATCH 6/7] rename the test; --- tests/Validation/ValidationValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index e285f8268289..4003c4b72483 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -8199,7 +8199,7 @@ public function testExclude($rules, $data, $expectedValidatedData) $this->assertSame($expectedValidatedData, $validator->validated()); } - public function testExcludeBeforeRequiredIf() + public function testExcludeBeforeADependentRule() { $validator = new Validator( $this->getIlluminateArrayTranslator(), From 7c8b495fc6584aee195720982a52c5833e7b3e23 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 27 Aug 2023 10:41:06 -0500 Subject: [PATCH 7/7] formatting --- src/Illuminate/Validation/Validator.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index f68a3e9d6cb5..80637ce90c2c 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -438,8 +438,7 @@ public function passes() } } } - // Only remove attributes after having looped through all the rules, so that - // the order of the attributes with applied "exclude" rule doesn't matter. + foreach ($this->rules as $attribute => $rules) { if ($this->shouldBeExcluded($attribute)) { $this->removeAttribute($attribute);