From 90be153a058a29aec443ae93f1d85178efd977e3 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Fri, 13 Aug 2021 22:20:49 +0800 Subject: [PATCH 1/4] Add method sometimesNested() to validator --- src/Illuminate/Validation/Validator.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index f5f1a8fbf8eb..0cb3f5278241 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1112,6 +1112,31 @@ public function sometimes($attribute, $rules, callable $callback) return $this; } + /** + * Add conditions to a given nested field based on a Closure. + * + * @param string $parent + * @param string|array $attribute + * @param string|array $rules + * @param callable $callback + * @return $this + */ + function sometimesNested(string $parent, $attribute, $rules, callable $callback) + { + foreach ((array) $this->getValue($parent) as $index => $item) { + + $payload = new Fluent($item); + + if ($callback($payload)) { + foreach ((array) $attribute as $key) { + $this->addRules([$parent . '.' . $index . '.' . $key => $rules]); + } + } + } + + return $this; + } + /** * Instruct the validator to stop validating after the first rule failure. * From b2b6404faaaacc4725432dca930297bfac8ce6c0 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Fri, 13 Aug 2021 22:21:11 +0800 Subject: [PATCH 2/4] Add tests for sometimesNested() --- tests/Validation/ValidationValidatorTest.php | 103 +++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 876756d97c6c..4ace39bfbe46 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4061,6 +4061,109 @@ public function testSometimesAddingRules() $this->assertEquals(['foo.0.name' => ['Required', 'String']], $v->getRules()); } + public function testSometimesNestedAddingRules() + { + $data = [ + 'first' => [ + [ + 'type' => 'email', + 'value' => 'test@test.com', + 'description' => 'Foo Bar', + ], + [ + 'type' => 'email', + 'value' => 'No valid email', + 'description' => 'Foo Bar', + ], + [ + 'type' => 'url', + 'value' => 'https://www.laraval.com/', + 'description' => 'Foo Bar', + ], + [ + 'type' => 'url', + 'value' => 'No valid URL', + 'description' => 'Foo Bar', + ], + [ + 'type' => 'string', + 'value' => 'Valid string', + 'description' => 'Foo Bar', + ], + ], + 'second' => [ + 'deep' => [ + [ + 'type' => 'email', + 'value' => 'test@test.com', + 'description' => 'Foo Bar', + ], + [ + 'type' => 'string', + 'value' => 'Valid string', + 'description' => 'Foo Bar', + ], + ], + ], + ]; + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.4.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'String', function ($i) { + return $i->type === 'url'; + }); + $this->assertNotEquals(['first.4.value' => ['Required', 'String']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.4.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'String', function ($i) { + return $i->type === 'string'; + }); + $this->assertEquals(['first.4.value' => ['Required', 'String']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.4.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { + return $i->type === 'email'; + }); + $this->assertNotEquals(['first.4.value' => ['Required', 'String']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.0.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { + return $i->type === 'email'; + }); + $this->assertNotEquals(['first.4.value' => ['Required', 'String']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.0.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { + return $i->type === 'email'; + }); + $this->assertNotEquals(['first.4.value' => ['Required', 'String']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.4.value' => ['Required']]); + $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { + return $i->type === 'email'; + }); + $this->assertEquals(['first.0.value' => ['email:rfc,dns'], 'first.1.value' => ['email:rfc,dns'],'first.4.value' => ['Required']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['first.0.type' => ['Required']]); + $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { + return true; + }); + $this->assertNotEquals(['first.0.type' => ['Required'], 'first.4.type' => ['email:rfc,dns']], $v->getRules()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, $data, ['second.deep.0.value' => ['Required'], 'second.deep.1.value' => ['Required']]); + $v->sometimesNested('second.deep', 'value', 'email:rfc,dns', function ($i) { + return $i->type === 'email'; + }); + $this->assertEquals(['second.deep.0.value' => ['Required', 'email:rfc,dns'], 'second.deep.1.value' => ['Required']], $v->getRules()); + } + public function testCustomValidators() { $trans = $this->getIlluminateArrayTranslator(); From cb24e3ad4109ceee8a99c962c6980edf1ef9c158 Mon Sep 17 00:00:00 2001 From: NickSdot Date: Sun, 15 Aug 2021 12:21:47 +0800 Subject: [PATCH 3/4] Style fixes --- src/Illuminate/Validation/Validator.php | 4 ++-- tests/Validation/ValidationValidatorTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 0cb3f5278241..772b17a7202f 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1121,7 +1121,7 @@ public function sometimes($attribute, $rules, callable $callback) * @param callable $callback * @return $this */ - function sometimesNested(string $parent, $attribute, $rules, callable $callback) + public function sometimesNested(string $parent, $attribute, $rules, callable $callback) { foreach ((array) $this->getValue($parent) as $index => $item) { @@ -1129,7 +1129,7 @@ function sometimesNested(string $parent, $attribute, $rules, callable $callback) if ($callback($payload)) { foreach ((array) $attribute as $key) { - $this->addRules([$parent . '.' . $index . '.' . $key => $rules]); + $this->addRules([$parent.'.'.$index.'.'.$key => $rules]); } } } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 4ace39bfbe46..4bb1d9b63d8c 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4108,7 +4108,7 @@ public function testSometimesNestedAddingRules() ]; $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, $data, ['first.4.value' => ['Required']]); + $v = new Validator($trans, $data, ['first.2.value' => ['Required'], 'first.4.value' => ['Required']]); $v->sometimesNested('first', 'value', 'String', function ($i) { return $i->type === 'url'; }); @@ -4147,7 +4147,7 @@ public function testSometimesNestedAddingRules() $v->sometimesNested('first', 'value', 'email:rfc,dns', function ($i) { return $i->type === 'email'; }); - $this->assertEquals(['first.0.value' => ['email:rfc,dns'], 'first.1.value' => ['email:rfc,dns'],'first.4.value' => ['Required']], $v->getRules()); + $this->assertEquals(['first.0.value' => ['email:rfc,dns'], 'first.1.value' => ['email:rfc,dns'], 'first.4.value' => ['Required']], $v->getRules()); $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, $data, ['first.0.type' => ['Required']]); From 007a2f5250d810d5c6305f1577a15b65e277bb8c Mon Sep 17 00:00:00 2001 From: NickSdot Date: Sun, 15 Aug 2021 12:25:32 +0800 Subject: [PATCH 4/4] Style fixes --- 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 772b17a7202f..019305324286 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1124,7 +1124,6 @@ public function sometimes($attribute, $rules, callable $callback) public function sometimesNested(string $parent, $attribute, $rules, callable $callback) { foreach ((array) $this->getValue($parent) as $index => $item) { - $payload = new Fluent($item); if ($callback($payload)) {