Skip to content

Commit

Permalink
[8.x] Retain the original attribute value during validation of an arr…
Browse files Browse the repository at this point in the history
…ay 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 <[email protected]>
  • Loading branch information
chiefey and stackbrian authored May 24, 2022
1 parent 141cf12 commit aabf205
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ public function addFailure($attribute, $rule, $parameters = [])
$this->passes();
}

$attributeWithPlaceholders = $attribute;

$attribute = str_replace(
[$this->dotPlaceholder, '__asterisk__'],
['.', '*'],
Expand All @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit aabf205

Please sign in to comment.