Skip to content

Commit 7330b92

Browse files
authoredJun 24, 2024
[11.x] Fix validation attributes when translations are empty or missing (#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
1 parent 5ed0ce8 commit 7330b92

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed
 

‎src/Illuminate/Validation/Concerns/FormatsMessages.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@ public function getDisplayableAttribute($attribute)
305305
*/
306306
protected function getAttributeFromTranslations($name)
307307
{
308-
return $this->getAttributeFromLocalArray(
309-
$name, Arr::dot((array) $this->translator->get('validation.attributes'))
310-
);
308+
if (! is_array($attributes = $this->translator->get('validation.attributes'))) {
309+
return null;
310+
}
311+
312+
return $this->getAttributeFromLocalArray($name, Arr::dot($attributes));
311313
}
312314

313315
/**

‎tests/Validation/ValidationValidatorTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,20 @@ public function testTranslatedAttributeNamesAreReplacedInArraysFromNestedRules()
592592
$this->assertSame('User ID is required!', $v->messages()->first('users.0.id'));
593593
}
594594

595+
public function testTranslatedAttributesCanBeMissing()
596+
{
597+
$trans = $this->getIlluminateArrayTranslator();
598+
$trans->addLines(['validation.gt.numeric' => ':attribute must be greater than :value.'], 'en');
599+
$trans->addLines(['validation.attributes' => []], 'en');
600+
$v = new Validator($trans, ['total' => 0], ['total' => 'gt:0']);
601+
$this->assertSame('total must be greater than 0.', $v->messages()->first('total'));
602+
603+
$trans = $this->getIlluminateArrayTranslator();
604+
$trans->addLines(['validation.gt.numeric' => ':attribute must be greater than :value.'], 'en');
605+
$v = new Validator($trans, ['total' => 0], ['total' => 'gt:0']);
606+
$this->assertSame('total must be greater than 0.', $v->messages()->first('total'));
607+
}
608+
595609
public function testInputIsReplaced()
596610
{
597611
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)