Skip to content

Commit

Permalink
Provide context for NestedRules (#51160)
Browse files Browse the repository at this point in the history
  • Loading branch information
imahmood authored Apr 22, 2024
1 parent 134db76 commit 1355ba2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Validation/NestedRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public function __construct(callable $callback)
* @param string $attribute
* @param mixed $value
* @param mixed $data
* @param mixed $context
* @return \stdClass
*/
public function compile($attribute, $value, $data = null)
public function compile($attribute, $value, $data = null, $context = null)
{
$rules = call_user_func($this->callback, $value, $attribute, $data);
$rules = call_user_func($this->callback, $value, $attribute, $data, $context);

$parser = new ValidationRuleParser(
Arr::undot(Arr::wrap($data))
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected function prepareRule($rule, $attribute)

if ($rule instanceof NestedRules) {
return $rule->compile(
$attribute, $this->data[$attribute] ?? null, Arr::dot($this->data)
$attribute, $this->data[$attribute] ?? null, Arr::dot($this->data), $this->data
)->rules[$attribute];
}

Expand All @@ -152,7 +152,9 @@ protected function explodeWildcardRules($results, $attribute, $rules)
if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {
foreach ((array) $rules as $rule) {
if ($rule instanceof NestedRules) {
$compiled = $rule->compile($key, $value, $data);
$context = Arr::get($this->data, Str::beforeLast($key, '.'));

$compiled = $rule->compile($key, $value, $data, $context);

$this->implicitAttributes = array_merge_recursive(
$compiled->implicitAttributes,
Expand Down
11 changes: 7 additions & 4 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,16 @@ public function testExplodeGeneratesNestedRules()
{
$parser = (new ValidationRuleParser([
'users' => [
['name' => 'Taylor Otwell'],
['name' => 'Taylor Otwell', 'email' => '[email protected]'],
],
]));

$results = $parser->explode([
'users.*.name' => Rule::forEach(function ($value, $attribute, $data) {
'users.*.name' => Rule::forEach(function ($value, $attribute, $data, $context) {
$this->assertSame('Taylor Otwell', $value);
$this->assertSame('users.0.name', $attribute);
$this->assertEquals($data['users.0.name'], 'Taylor Otwell');
$this->assertEquals(['name' => 'Taylor Otwell', 'email' => '[email protected]'], $context);

return [Rule::requiredIf(true)];
}),
Expand All @@ -201,13 +202,15 @@ public function testExplodeGeneratesNestedRulesForNonNestedData()
{
$parser = (new ValidationRuleParser([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
]));

$results = $parser->explode([
'name' => Rule::forEach(function ($value, $attribute, $data = null) {
'name' => Rule::forEach(function ($value, $attribute, $data = null, $context) {
$this->assertSame('Taylor Otwell', $value);
$this->assertSame('name', $attribute);
$this->assertEquals(['name' => 'Taylor Otwell'], $data);
$this->assertEquals(['name' => 'Taylor Otwell', 'email' => '[email protected]'], $data);
$this->assertEquals(['name' => 'Taylor Otwell', 'email' => '[email protected]'], $context);

return 'required';
}),
Expand Down

0 comments on commit 1355ba2

Please sign in to comment.