From 83e28d065b7bc53f3e53a5c188806844eee30161 Mon Sep 17 00:00:00 2001 From: Adrien Foulon <6115458+Tofandel@users.noreply.github.com> Date: Thu, 30 May 2024 16:45:43 +0200 Subject: [PATCH] Merge preserving keys only one level deep (#51516) --- src/Illuminate/Validation/Validator.php | 6 ++--- tests/Validation/ValidationValidatorTest.php | 23 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index 2dff098c11a2..d20dcc60cb50 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -1205,9 +1205,9 @@ public function addRules($rules) $response = (new ValidationRuleParser($this->data)) ->explode(ValidationRuleParser::filterConditionalRules($rules, $this->data)); - $this->rules = array_merge_recursive( - $this->rules, $response->rules - ); + foreach ($response->rules as $key => $rule) { + $this->rules[$key] = array_merge($this->rules[$key] ?? [], $rule); + } $this->implicitAttributes = array_merge( $this->implicitAttributes, $response->implicitAttributes diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 7defa977f82c..d0009f1cb7c6 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -5090,6 +5090,29 @@ public function testAlternativeFormat() $this->assertTrue($v->passes()); } + public function testNumericKeys() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['3' => 'aslsdlks'], [3 => 'required']); + $this->assertTrue($v->passes()); + } + + public function testMergeRules() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'asl', 'a' => [1, 4]], ['x' => ['alpha', ['min', 3]], 'a.*' => 'integer']); + $v->addRules(['x' => ['required', ['max', 10]], 'a.1' => 'digits:1']); + $this->assertEquals( + [ + 'x' => ['alpha', ['min', 3], 'required', ['max', 10]], + 'a.0' => ['integer'], + 'a.1' => ['integer', 'digits:1'], + ], + $v->getRules() + ); + $this->assertTrue($v->passes()); + } + public function testValidateAlpha() { $trans = $this->getIlluminateArrayTranslator();