From aabf205e51f0391cb8cb72fc7e8bef6ae78a6432 Mon Sep 17 00:00:00 2001 From: chiefey <66399064+chiefey@users.noreply.github.com> Date: Tue, 24 May 2022 12:51:01 -0400 Subject: [PATCH] [8.x] Retain the original attribute value during validation of an array key with a dot for correct failure message (#42395) * Retain original attribute key including placeholder for failure message * Isolate placeholder changes to getMessage>getSizeMessage path Co-authored-by: Brian Gutowski --- .../Validation/Concerns/FormatsMessages.php | 6 ++++- src/Illuminate/Validation/Validator.php | 4 +++- tests/Validation/ValidationValidatorTest.php | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index c95cd3a8051c..c7c9a1dcb8c6 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -20,6 +20,10 @@ trait FormatsMessages */ protected function getMessage($attribute, $rule) { + $attributeWithPlaceholders = $attribute; + + $attribute = $this->replacePlaceholderInString($attribute); + $inlineMessage = $this->getInlineMessage($attribute, $rule); // First we will retrieve the custom message for the validation rule if one @@ -46,7 +50,7 @@ protected function getMessage($attribute, $rule) // specific error message for the type of attribute being validated such // as a number, file or string which all have different message types. elseif (in_array($rule, $this->sizeRules)) { - return $this->getSizeMessage($attribute, $rule); + return $this->getSizeMessage($attributeWithPlaceholders, $rule); } // Finally, if no developer specified messages have been set, and no other diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index f79314da47a3..9cbc91b2ff3e 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -867,6 +867,8 @@ public function addFailure($attribute, $rule, $parameters = []) $this->passes(); } + $attributeWithPlaceholders = $attribute; + $attribute = str_replace( [$this->dotPlaceholder, '__asterisk__'], ['.', '*'], @@ -878,7 +880,7 @@ public function addFailure($attribute, $rule, $parameters = []) } $this->messages->add($attribute, $this->makeReplacements( - $this->getMessage($attribute, $rule), $attribute, $rule, $parameters + $this->getMessage($attributeWithPlaceholders, $rule), $attribute, $rule, $parameters )); $this->failedRules[$attribute][$rule] = $parameters; diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index add01e7d82ad..bde76a9672cb 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -7158,6 +7158,28 @@ public function testArrayKeysValidationFailsWithNotAnArray() ); } + public function testArrayKeysWithDotIntegerMin() + { + $trans = $this->getIlluminateArrayTranslator(); + + $data = [ + 'foo.bar' => -1, + ]; + + $rules = [ + 'foo\.bar' => 'integer|min:1', + ]; + + $expectedResult = [ + 'foo.bar' => [ + 'validation.min.numeric', + ], + ]; + + $validator = new Validator($trans, $data, $rules, [], []); + $this->assertEquals($expectedResult, $validator->getMessageBag()->getMessages()); + } + protected function getTranslator() { return m::mock(TranslatorContract::class);