Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Preserve numeric keys on the first level of the validator rules #51516

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need array_merge, given that rules are always nested only 2 levels deep and are always associative arrays (Asserted by the ValidationRuleParser)

}

$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