Skip to content

Commit

Permalink
[11.x] Fix validation attributes when translations are empty or missi…
Browse files Browse the repository at this point in the history
…ng (#51890)

* Check custom attributes array is set

* Add test for when translated validation attributes are missing

* Skip translated attributes if not set or empty

* StyleCI fixes
  • Loading branch information
owenandrews authored Jun 24, 2024
1 parent 5ed0ce8 commit 7330b92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ public function getDisplayableAttribute($attribute)
*/
protected function getAttributeFromTranslations($name)
{
return $this->getAttributeFromLocalArray(
$name, Arr::dot((array) $this->translator->get('validation.attributes'))
);
if (! is_array($attributes = $this->translator->get('validation.attributes'))) {
return null;
}

return $this->getAttributeFromLocalArray($name, Arr::dot($attributes));
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ public function testTranslatedAttributeNamesAreReplacedInArraysFromNestedRules()
$this->assertSame('User ID is required!', $v->messages()->first('users.0.id'));
}

public function testTranslatedAttributesCanBeMissing()
{
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.gt.numeric' => ':attribute must be greater than :value.'], 'en');
$trans->addLines(['validation.attributes' => []], 'en');
$v = new Validator($trans, ['total' => 0], ['total' => 'gt:0']);
$this->assertSame('total must be greater than 0.', $v->messages()->first('total'));

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.gt.numeric' => ':attribute must be greater than :value.'], 'en');
$v = new Validator($trans, ['total' => 0], ['total' => 'gt:0']);
$this->assertSame('total must be greater than 0.', $v->messages()->first('total'));
}

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

0 comments on commit 7330b92

Please sign in to comment.