Skip to content

[12.x] Add possibility to define nested validation rules as array #55491

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

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 26 additions & 0 deletions src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ protected function explodeRules($rules)
if (str_contains($key, '*')) {
$rules = $this->explodeWildcardRules($rules, $key, [$rule]);

unset($rules[$key]);
} elseif (is_array($rule) && ! array_is_list($rule)) {
$rules = $this->explodeNestedRule($rules, $rule, $key);

unset($rules[$key]);
} else {
$rules[$key] = $this->explodeExplicitRule($rule, $key);
Expand All @@ -80,6 +84,28 @@ protected function explodeRules($rules)
return $rules;
}

/**
* Explode the nested rule into multiple rules using dot notation.
*
* @param array $results
* @param mixed $rule
* @param string $attribute
* @return array
*/
protected function explodeNestedRule($results, $rule, $attribute)
{
$input = $this->data[$attribute];
$prefix = (is_array($input) && ! array_is_list($input)) || is_object($input)
? $attribute.'.'
: $attribute.'.*.';
foreach ($rule as $key => $value) {
$dotKey = $prefix.$key;
$results = $this->mergeRules($results, $dotKey, $this->explodeExplicitRule($value, $dotKey));
}

return $results;
}

/**
* Explode the explicit rule into an array if necessary.
*
Expand Down
72 changes: 72 additions & 0 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,76 @@ public function testExplodeHandlesNumericRuleWithAdditionalRules()
],
], $results->rules);
}

public function testNestedValidationRulesObject()
{
$parser = (new ValidationRuleParser([
'number' => (object) [
'favorite' => 42,
],
]));

$rules = [
'number' => [
'favorite' => Rule::numeric()->max(100),
],
];

$results = $parser->explode($rules);

$this->assertEquals([
'number.favorite' => [
'numeric',
'max:100',
],
], $results->rules);
}

public function testNestedValidationRulesAssociativeArray()
{
$parser = (new ValidationRuleParser([
'number' => [
'favorite' => 42,
],
]));

$rules = [
'number' => [
'favorite' => Rule::numeric()->max(100),
],
];

$results = $parser->explode($rules);

$this->assertEquals([
'number.favorite' => [
'numeric',
'max:100',
],
], $results->rules);
}

public function testNestedValidationRulesNumericArray()
{
$parser = (new ValidationRuleParser([
'number' => [
['favorite' => 42],
],
]));

$rules = [
'number' => [
'favorite' => Rule::numeric()->max(100),
],
];

$results = $parser->explode($rules);

$this->assertEquals([
'number.*.favorite' => [
'numeric',
'max:100',
],
], $results->rules);
}
}