From f9c579a957d18016f8e6d8de24d784678dc73d4f Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 02:42:26 -0300 Subject: [PATCH 1/6] Fixes array to string conversion when using custom validation messages for size rules. --- .../Validation/Concerns/FormatsMessages.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index f433a5361eec..0ddd19e3f565 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -39,6 +39,9 @@ protected function getMessage($attribute, $rule) // and rule. This allows the developer to specify specific messages for // only some attributes and rules that need to get specially formed. if ($customMessage !== $customKey) { + if (in_array($rule, $this->sizeRules)) { + return $this->getCustomSizeMessage($attribute, $rule); + } return $customMessage; } @@ -178,6 +181,27 @@ protected function getSizeMessage($attribute, $rule) return $this->translator->get($key); } + + /** + * Get the proper error message for an attribute and size rule when using custom messages. + * + * @param string $attribute + * @param string $rule + * @return string + */ + protected function getCustomSizeMessage($attribute, $rule) + { + $lowerRule = Str::snake($rule); + + // There are three different types of size validations. The attribute may be + // either a number, file, or string so we will check a few things to know + // which type of value it is and return the correct line for that type. + $type = $this->getAttributeType($attribute); + + $key = "validation.custom.{$attribute}.{$lowerRule}.{$type}"; + + return $this->translator->get($key); + } /** * Get the data type of the given attribute. From ac4aaa6a7acaa29f43e0b1310c54885dcee54c96 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 02:55:09 -0300 Subject: [PATCH 2/6] Apply fixes from styleci --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 0ddd19e3f565..8517d583624c 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -42,6 +42,7 @@ protected function getMessage($attribute, $rule) if (in_array($rule, $this->sizeRules)) { return $this->getCustomSizeMessage($attribute, $rule); } + return $customMessage; } @@ -181,7 +182,7 @@ protected function getSizeMessage($attribute, $rule) return $this->translator->get($key); } - + /** * Get the proper error message for an attribute and size rule when using custom messages. * From 965179d00982d44df62b87f13490ed18da91dea7 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 10:04:29 -0300 Subject: [PATCH 3/6] Improve solution --- .../Validation/Concerns/FormatsMessages.php | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 8517d583624c..a5983d111d24 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,18 +31,16 @@ 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 // only some attributes and rules that need to get specially formed. if ($customMessage !== $customKey) { - if (in_array($rule, $this->sizeRules)) { - return $this->getCustomSizeMessage($attribute, $rule); - } - return $customMessage; } @@ -183,27 +181,6 @@ protected function getSizeMessage($attribute, $rule) return $this->translator->get($key); } - /** - * Get the proper error message for an attribute and size rule when using custom messages. - * - * @param string $attribute - * @param string $rule - * @return string - */ - protected function getCustomSizeMessage($attribute, $rule) - { - $lowerRule = Str::snake($rule); - - // There are three different types of size validations. The attribute may be - // either a number, file, or string so we will check a few things to know - // which type of value it is and return the correct line for that type. - $type = $this->getAttributeType($attribute); - - $key = "validation.custom.{$attribute}.{$lowerRule}.{$type}"; - - return $this->translator->get($key); - } - /** * Get the data type of the given attribute. * From dfa542af09c0666707694e009ee3ca9cd3a7c5b6 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 19:45:38 -0300 Subject: [PATCH 4/6] Adding tests --- tests/Validation/ValidationValidatorTest.php | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index d1d3d424dd59..42d2c9b16260 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -598,6 +598,28 @@ 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' => [ + 'name' => [ + 'between' => [ + 'numeric' => 'The :attribute must be between :min and :max.', + 'file' => 'The :attribute must be between :min and :max kilobytes.', + 'string' => 'Some custom message here', + 'array' => 'The :attribute must have between :min and :max items.', + ] + ] + ] + ]); + $v = new Validator($trans, ['name' => ''], ['name' => 'between:5,20']); + $this->assertFalse($v->passes()); + $v->messages()->setFormat(':message'); + $this->assertSame('Some custom message here', $v->messages()->first('name')); + } + public function testCustomValidationLinesAreRespectedWithAsterisks() { $trans = $this->getIlluminateArrayTranslator(); From 8a39e359ba695ba1a5f0bec4c7c510a6c06a7da1 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 19:53:25 -0300 Subject: [PATCH 5/6] fix tests --- tests/Validation/ValidationValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 42d2c9b16260..5b9964ffe686 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -614,7 +614,7 @@ public function testCustomValidationLinesForSizeRules() ] ] ]); - $v = new Validator($trans, ['name' => ''], ['name' => 'between:5,20']); + $v = new Validator($trans, ['name' => 'Taylor'], ['name' => 'between:10,20']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertSame('Some custom message here', $v->messages()->first('name')); From c09b7142d04f3d76193c08a7502b8b3b8082f7d6 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Tue, 6 Apr 2021 19:55:16 -0300 Subject: [PATCH 6/6] add fixes from styleci --- tests/Validation/ValidationValidatorTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 5b9964ffe686..fef4ee42069c 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -610,9 +610,9 @@ public function testCustomValidationLinesForSizeRules() 'file' => 'The :attribute must be between :min and :max kilobytes.', 'string' => 'Some custom message here', 'array' => 'The :attribute must have between :min and :max items.', - ] - ] - ] + ], + ], + ], ]); $v = new Validator($trans, ['name' => 'Taylor'], ['name' => 'between:10,20']); $this->assertFalse($v->passes());