Skip to content

Commit

Permalink
Merge preserving keys only one level deep (laravel#51516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel authored May 30, 2024
1 parent 5611359 commit 83e28d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 83e28d0

Please sign in to comment.