From 50d22c02cffb2f4d7535f2fec887ade513c4ad66 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Wed, 22 Sep 2021 06:45:31 +0800 Subject: [PATCH 1/4] Hotfix for https://github.com/laravel/framework/pull/38443#issuecomment-923439555 --- src/Illuminate/Validation/Validator.php | 6 ++- tests/Validation/ValidationValidatorTest.php | 46 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index c10fafb3f84f..3e5ebe87378d 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1127,7 +1127,11 @@ public function sometimes($attribute, $rules, callable $callback) foreach ($response->rules as $ruleKey => $ruleValue) { if ($callback($payload, $this->dataForSometimesIteration($ruleKey, ! Str::endsWith($key, '.*')))) { - $this->addRules([$ruleKey => $ruleValue]); + if(is_array($rules)) { + $this->addRules([$key => $ruleValue]); + } else { + $this->addRules([$ruleKey => $ruleValue]); + } } } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 42ba9bdeaa7b..d49879f5ec56 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4378,6 +4378,52 @@ public function testItemAwareSometimesAddingRules() $this->assertEquals(['attendee.name' => ['string', 'required'], 'attendee.title' => ['string', 'required'], 'attendee.type' => ['string', 'required']], $v->getRules()); } + public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter() + { + $trans = $this->getIlluminateArrayTranslator(); + + $v = new Validator($trans, [ + 'foo' => [ + ['start' => '2016-04-19', 'end' => '2017-04-19'], + ], + ], []); + $v->sometimes('foo.*.start', ['before:foo.*.end'], function () { + return true; + }); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, [ + 'foo' => [ + ['start' => '2016-04-19', 'end' => '2017-04-19'], + ], + ], []); + $v->sometimes('foo.*.end', ['before:foo.*.start'], function () { + return true; + }); + + $this->assertTrue($v->fails()); + + $v = new Validator($trans, [ + 'foo' => [ + ['start' => '2016-04-19', 'end' => '2017-04-19'], + ], + ], []); + $v->sometimes('foo.*.end', ['after:foo.*.start'], function () { + return true; + }); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, [ + 'foo' => [ + ['start' => '2016-04-19', 'end' => '2017-04-19'], + ], + ], []); + $v->sometimes('foo.*.start', ['after:foo.*.end'], function () { + return true; + }); + $this->assertTrue($v->fails()); + } + public function testCustomValidators() { $trans = $this->getIlluminateArrayTranslator(); From eb4118f9373d6f1b6074f5c300730d5ac1cdb15c Mon Sep 17 00:00:00 2001 From: NickSdot Date: Wed, 22 Sep 2021 07:05:05 +0800 Subject: [PATCH 2/4] Fix style --- src/Illuminate/Validation/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 3e5ebe87378d..742088fc5b6e 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1127,7 +1127,7 @@ public function sometimes($attribute, $rules, callable $callback) foreach ($response->rules as $ruleKey => $ruleValue) { if ($callback($payload, $this->dataForSometimesIteration($ruleKey, ! Str::endsWith($key, '.*')))) { - if(is_array($rules)) { + if (is_array($rules)) { $this->addRules([$key => $ruleValue]); } else { $this->addRules([$ruleKey => $ruleValue]); From 25cdda6606eb3e682594d976f1d7c84128162f0e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Sep 2021 09:38:32 -0500 Subject: [PATCH 3/4] fix bug --- src/Illuminate/Validation/Validator.php | 8 +++----- tests/Validation/ValidationValidatorTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 742088fc5b6e..bc7abe2bf72e 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1125,13 +1125,11 @@ public function sometimes($attribute, $rules, callable $callback) foreach ((array) $attribute as $key) { $response = (new ValidationRuleParser($this->data))->explode([$key => $rules]); + $this->implicitAttributes = array_merge($response->implicitAttributes, $this->implicitAttributes); + foreach ($response->rules as $ruleKey => $ruleValue) { if ($callback($payload, $this->dataForSometimesIteration($ruleKey, ! Str::endsWith($key, '.*')))) { - if (is_array($rules)) { - $this->addRules([$key => $ruleValue]); - } else { - $this->addRules([$ruleKey => $ruleValue]); - } + $this->addRules([$ruleKey => $ruleValue]); } } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index d49879f5ec56..fb7dcad9b1f3 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4392,6 +4392,18 @@ public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter() }); $this->assertTrue($v->passes()); + return; + + $v = new Validator($trans, [ + 'foo' => [ + ['start' => '2016-04-19', 'end' => '2017-04-19'], + ], + ], []); + $v->sometimes('foo.*.start', 'before:foo.*.end', function () { + return true; + }); + $this->assertTrue($v->passes()); + $v = new Validator($trans, [ 'foo' => [ ['start' => '2016-04-19', 'end' => '2017-04-19'], From a2b8c5ae87f44c80881eaf691e69e59bc79ca483 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 23 Sep 2021 13:44:04 -0500 Subject: [PATCH 4/4] Update ValidationValidatorTest.php --- tests/Validation/ValidationValidatorTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index fb7dcad9b1f3..934e6e3a7fd0 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4392,8 +4392,6 @@ public function testValidateSometimesImplicitEachWithAsterisksBeforeAndAfter() }); $this->assertTrue($v->passes()); - return; - $v = new Validator($trans, [ 'foo' => [ ['start' => '2016-04-19', 'end' => '2017-04-19'],