Skip to content
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
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' => 'taylor@laravel.com'],
],
]));

$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' => 'taylor@laravel.com'], $context);

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

$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' => 'taylor@laravel.com'], $data);
$this->assertEquals(['name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com'], $context);

return 'required';
}),
Expand Down