From cfcf2534e9d8469b54d07d532eafda5bb9d65004 Mon Sep 17 00:00:00 2001 From: mateusjunges Date: Thu, 16 Dec 2021 10:50:38 -0300 Subject: [PATCH 1/4] fix array to string conversion when using custom validation messages for size rules --- .../Validation/Concerns/FormatsMessages.php | 8 +++--- tests/Validation/ValidationValidatorTest.php | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index c95cd3a8051c..324f2d9fe66b 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,9 +31,11 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); - $customMessage = $this->getCustomMessageFromTranslator( - $customKey = "validation.custom.{$attribute}.{$lowerRule}" - ); + $customKey = in_array($rule, $this->sizeRules) + ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" + : "validation.custom.{$attribute}.{$lowerRule}"; + + $customMessage = $this->getCustomMessageFromTranslator($customKey); // First we check for a custom defined validation message for the attribute // and rule. This allows the developer to specify specific messages for diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index a7da88c4fdd6..523a5f05d83e 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -600,6 +600,31 @@ public function testCustomValidationLinesAreRespected() $this->assertSame('really required!', $v->messages()->first('name')); } + public function testCustomValidationLinesForSizeRules() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->getLoader()->addMessages('en', 'validation', [ + 'required' => 'required!', + 'custom' => [ + 'image' => [ + 'gte' => [ + 'file' => 'Custom message for image files.', + 'string' => 'Custom message for image filenames.', + ], + ] + ], + ]); + + $v = new Validator($trans, ['image' => 'image.png'], ['image' => 'gte:50']); + $this->assertFalse($v->passes()); + $this->assertSame('Custom message for image filenames.', $v->messages()->first('image')); + + $file = new UploadedFile(__FILE__, '', null, null, true); + $v = new Validator($trans, ['image' => $file], ['image' => 'gte:50']); + $this->assertFalse($v->passes()); + $this->assertSame('Custom message for image files.', $v->messages()->first('image')); + } + public function testCustomValidationLinesAreRespectedWithAsterisks() { $trans = $this->getIlluminateArrayTranslator(); From 70bf174d8ce069c196fcc44966881bc53769a6df Mon Sep 17 00:00:00 2001 From: mateusjunges Date: Thu, 16 Dec 2021 11:23:58 -0300 Subject: [PATCH 2/4] ci fixes --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 7 ++++--- tests/Validation/ValidationValidatorTest.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 324f2d9fe66b..bf69ac1d60e1 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,9 +31,10 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); - $customKey = in_array($rule, $this->sizeRules) - ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" - : "validation.custom.{$attribute}.{$lowerRule}"; +// $customKey = in_array($rule, $this->sizeRules) +// ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" +// : "validation.custom.{$attribute}.{$lowerRule}"; + $customKey = "validation.custom.{$attribute}.{$lowerRule}"; $customMessage = $this->getCustomMessageFromTranslator($customKey); diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 523a5f05d83e..fff9e6f0f0fb 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -611,7 +611,7 @@ public function testCustomValidationLinesForSizeRules() 'file' => 'Custom message for image files.', 'string' => 'Custom message for image filenames.', ], - ] + ], ], ]); From e9579232c1e3e44463893b151cbc9aa3464e3a5d Mon Sep 17 00:00:00 2001 From: mateusjunges Date: Thu, 16 Dec 2021 11:24:36 -0300 Subject: [PATCH 3/4] remove missing comment --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index bf69ac1d60e1..324f2d9fe66b 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,10 +31,9 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); -// $customKey = in_array($rule, $this->sizeRules) -// ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" -// : "validation.custom.{$attribute}.{$lowerRule}"; - $customKey = "validation.custom.{$attribute}.{$lowerRule}"; + $customKey = in_array($rule, $this->sizeRules) + ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" + : "validation.custom.{$attribute}.{$lowerRule}"; $customMessage = $this->getCustomMessageFromTranslator($customKey); From 128fcf8451afa690446748da296629e9fbee5016 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 16 Dec 2021 10:26:14 -0600 Subject: [PATCH 4/4] formatting --- .../Validation/Concerns/FormatsMessages.php | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 324f2d9fe66b..d0da5c009171 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,11 +31,13 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); - $customKey = in_array($rule, $this->sizeRules) - ? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}" - : "validation.custom.{$attribute}.{$lowerRule}"; + $customKey = "validation.custom.{$attribute}.{$lowerRule}"; - $customMessage = $this->getCustomMessageFromTranslator($customKey); + $customMessage = $this->getCustomMessageFromTranslator( + in_array($rule, $this->sizeRules) + ? [$customKey.".{$this->getAttributeType($attribute)}", $customKey] + : $customKey + ); // First we check for a custom defined validation message for the attribute // and rule. This allows the developer to specify specific messages for @@ -120,25 +122,33 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null) /** * Get the custom error message from the translator. * - * @param string $key + * @param array|string $keys * @return string */ - protected function getCustomMessageFromTranslator($key) + protected function getCustomMessageFromTranslator($keys) { - if (($message = $this->translator->get($key)) !== $key) { - return $message; - } + foreach (Arr::wrap($keys) as $key) { + if (($message = $this->translator->get($key)) !== $key) { + return $message; + } - // If an exact match was not found for the key, we will collapse all of these - // messages and loop through them and try to find a wildcard match for the - // given key. Otherwise, we will simply return the key's value back out. - $shortKey = preg_replace( - '/^validation\.custom\./', '', $key - ); + // If an exact match was not found for the key, we will collapse all of these + // messages and loop through them and try to find a wildcard match for the + // given key. Otherwise, we will simply return the key's value back out. + $shortKey = preg_replace( + '/^validation\.custom\./', '', $key + ); + + $message = $this->getWildcardCustomMessages(Arr::dot( + (array) $this->translator->get('validation.custom') + ), $shortKey, $key); + + if ($message !== $key) { + return $message; + } + } - return $this->getWildcardCustomMessages(Arr::dot( - (array) $this->translator->get('validation.custom') - ), $shortKey, $key); + return Arr::last(Arr::wrap($keys)); } /**