Skip to content

Commit

Permalink
fix validation nested error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored and driesvints committed Jul 10, 2020
1 parent d6fc44b commit 6615371
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null)
// that is not attribute specific. If we find either we'll return it.
foreach ($keys as $key) {
foreach (array_keys($source) as $sourceKey) {
if (strpos($sourceKey, '*') !== false) {
$pattern = str_replace('\*', '([^.]*)', preg_quote($sourceKey, '#'));

if (preg_match('#^'.$pattern.'\z#u', $key) === 1) {
return $source[$sourceKey];
}

continue;
}

if (Str::is($sourceKey, $key)) {
return $source[$sourceKey];
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ protected function tearDown(): void
m::close();
}

public function testNestedErrorMessagesAreRetrievedFromLocalArray()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [
'users' => [
[
'name' => 'Taylor Otwell',
'posts' => [
[
'name' => '',
]
],
],
],
], [
'users.*.name' => ['required'],
'users.*.posts.*.name' => ['required'],
], [
'users.*.name.required' => 'user name is required',
'users.*.posts.*.name.required' => 'post name is required',
]);

$this->assertFalse($v->passes());
$this->assertEquals('post name is required', $v->errors()->all()[0]);
}

public function testSometimesWorksOnNestedArrays()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 6615371

Please sign in to comment.