Skip to content

Commit 478c719

Browse files
committed
Merge preserving keys only one level deep
1 parent f22eaf2 commit 478c719

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Illuminate/Validation/Validator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,9 @@ public function addRules($rules)
12051205
$response = (new ValidationRuleParser($this->data))
12061206
->explode(ValidationRuleParser::filterConditionalRules($rules, $this->data));
12071207

1208-
$this->rules = array_merge_recursive(
1209-
$this->rules, $response->rules
1210-
);
1208+
foreach ($response->rules as $key => $rule) {
1209+
$this->rules[$key] = array_merge($this->rules[$key] ?? [], $rule);
1210+
}
12111211

12121212
$this->implicitAttributes = array_merge(
12131213
$this->implicitAttributes, $response->implicitAttributes

tests/Validation/ValidationValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5090,6 +5090,29 @@ public function testAlternativeFormat()
50905090
$this->assertTrue($v->passes());
50915091
}
50925092

5093+
public function testNumericKeys()
5094+
{
5095+
$trans = $this->getIlluminateArrayTranslator();
5096+
$v = new Validator($trans, ['3' => 'aslsdlks'], [3 => 'required']);
5097+
$this->assertTrue($v->passes());
5098+
}
5099+
5100+
public function testMergeRules()
5101+
{
5102+
$trans = $this->getIlluminateArrayTranslator();
5103+
$v = new Validator($trans, ['x' => 'asl', 'a' => [1, 4]], ['x' => ['alpha', ['min', 3]], 'a.*' => 'integer']);
5104+
$v->addRules(['x' => ['required', ['max', 10]], 'a.1' => 'digits:1']);
5105+
$this->assertEquals(
5106+
[
5107+
'x' => ['alpha', ['min', 3], 'required', ['max', 10]],
5108+
'a.0' => ['integer'],
5109+
'a.1' => ['integer', 'digits:1'],
5110+
],
5111+
$v->getRules()
5112+
);
5113+
$this->assertTrue($v->passes());
5114+
}
5115+
50935116
public function testValidateAlpha()
50945117
{
50955118
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)