diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index c95cd3a8051c..d0da5c009171 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -31,8 +31,12 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); + $customKey = "validation.custom.{$attribute}.{$lowerRule}"; + $customMessage = $this->getCustomMessageFromTranslator( - $customKey = "validation.custom.{$attribute}.{$lowerRule}" + in_array($rule, $this->sizeRules) + ? [$customKey.".{$this->getAttributeType($attribute)}", $customKey] + : $customKey ); // First we check for a custom defined validation message for the attribute @@ -118,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)); } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index a7da88c4fdd6..fff9e6f0f0fb 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();