Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Retain the original attribute value during validation of an array key with a dot for correct failure message #42395

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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