diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 39d102a331ba..4f0148e5d8de 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -276,7 +276,23 @@ public function getDisplayableAttribute($attribute) */ protected function getAttributeFromTranslations($name) { - return Arr::get($this->translator->get('validation.attributes'), $name); + $attributes = $this->translator->get('validation.attributes'); + $mostSpecific = Arr::get($attributes, $name); + if ($mostSpecific !== null) { + return $mostSpecific; + } + + $tokens = array_reverse(explode('.', $name)); + while (! empty($tokens)) { + $key = implode('.', array_reverse($tokens)); + $translation = Arr::get($attributes, $key); + if ($translation !== null) { + return $translation; + } + array_pop($tokens); + } + + return null; } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 1772582d18ba..01ca6eb924e1 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -413,6 +413,13 @@ public function testAttributeNamesAreReplacedInArrays() $v = new Validator($trans, ['names' => [null, 'name']], ['names.*' => 'Required']); $v->messages()->setFormat(':message'); $this->assertSame('First name is required!', $v->messages()->first('names.0')); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.required' => ':attribute is required!'], 'en'); + $trans->addLines(['validation.attributes' => ['name' => 'Name']], 'en'); + $v = new Validator($trans, ['items' => [['name' => 'John'], []]], ['items.*.name' => 'Required']); + $v->messages()->setFormat(':message'); + $this->assertSame('Name is required!', $v->messages()->first('items.1.name')); } public function testInputIsReplaced()