From 4274b3dd32312988af2f28cf9a319dda8e8616a6 Mon Sep 17 00:00:00 2001 From: Owen Andrews Date: Sat, 22 Jun 2024 12:50:02 +1000 Subject: [PATCH 1/4] Check custom attributes array is set --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index c03e2636a51e..3055e4d9bf01 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -305,8 +305,12 @@ public function getDisplayableAttribute($attribute) */ protected function getAttributeFromTranslations($name) { + if (($attributes = $this->translator->get('validation.attributes')) === 'validation.attributes') { + return null; + } + return $this->getAttributeFromLocalArray( - $name, Arr::dot((array) $this->translator->get('validation.attributes')) + $name, Arr::dot((array) $attributes) ); } From 12baf5ffd1a30b6b0829c537863f59a4eee88c69 Mon Sep 17 00:00:00 2001 From: Owen Andrews Date: Sat, 22 Jun 2024 12:52:21 +1000 Subject: [PATCH 2/4] Add test for when translated validation attributes are missing --- tests/Validation/ValidationValidatorTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index af2c22a60c51..f77d1857a43e 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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(); From 90763f50ffff7ebb635b3728970f70ca6f049b90 Mon Sep 17 00:00:00 2001 From: Owen Andrews Date: Sat, 22 Jun 2024 13:02:24 +1000 Subject: [PATCH 3/4] Skip translated attributes if not set or empty --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 3055e4d9bf01..97ff1743cbe4 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -305,13 +305,11 @@ public function getDisplayableAttribute($attribute) */ protected function getAttributeFromTranslations($name) { - if (($attributes = $this->translator->get('validation.attributes')) === 'validation.attributes') { + if (!is_array($attributes = $this->translator->get('validation.attributes'))) { return null; } - - return $this->getAttributeFromLocalArray( - $name, Arr::dot((array) $attributes) - ); + + return $this->getAttributeFromLocalArray($name, Arr::dot($attributes)); } /** From 14fcd919d18568694538c4695b706b844fdc14e3 Mon Sep 17 00:00:00 2001 From: Owen Andrews Date: Mon, 24 Jun 2024 20:47:47 +1000 Subject: [PATCH 4/4] StyleCI fixes --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 2 +- tests/Validation/ValidationValidatorTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 97ff1743cbe4..4fc6c1fa4807 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -305,7 +305,7 @@ public function getDisplayableAttribute($attribute) */ protected function getAttributeFromTranslations($name) { - if (!is_array($attributes = $this->translator->get('validation.attributes'))) { + if (! is_array($attributes = $this->translator->get('validation.attributes'))) { return null; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index f77d1857a43e..2a207bb8813c 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -598,12 +598,12 @@ public function testTranslatedAttributesCanBeMissing() $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')); + $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')); + $this->assertSame('total must be greater than 0.', $v->messages()->first('total')); } public function testInputIsReplaced()