Skip to content

Commit aabf205

Browse files
chiefeystackbrian
andauthored
[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 <[email protected]>
1 parent 141cf12 commit aabf205

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/Illuminate/Validation/Concerns/FormatsMessages.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ trait FormatsMessages
2020
*/
2121
protected function getMessage($attribute, $rule)
2222
{
23+
$attributeWithPlaceholders = $attribute;
24+
25+
$attribute = $this->replacePlaceholderInString($attribute);
26+
2327
$inlineMessage = $this->getInlineMessage($attribute, $rule);
2428

2529
// First we will retrieve the custom message for the validation rule if one
@@ -46,7 +50,7 @@ protected function getMessage($attribute, $rule)
4650
// specific error message for the type of attribute being validated such
4751
// as a number, file or string which all have different message types.
4852
elseif (in_array($rule, $this->sizeRules)) {
49-
return $this->getSizeMessage($attribute, $rule);
53+
return $this->getSizeMessage($attributeWithPlaceholders, $rule);
5054
}
5155

5256
// Finally, if no developer specified messages have been set, and no other

src/Illuminate/Validation/Validator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ public function addFailure($attribute, $rule, $parameters = [])
867867
$this->passes();
868868
}
869869

870+
$attributeWithPlaceholders = $attribute;
871+
870872
$attribute = str_replace(
871873
[$this->dotPlaceholder, '__asterisk__'],
872874
['.', '*'],
@@ -878,7 +880,7 @@ public function addFailure($attribute, $rule, $parameters = [])
878880
}
879881

880882
$this->messages->add($attribute, $this->makeReplacements(
881-
$this->getMessage($attribute, $rule), $attribute, $rule, $parameters
883+
$this->getMessage($attributeWithPlaceholders, $rule), $attribute, $rule, $parameters
882884
));
883885

884886
$this->failedRules[$attribute][$rule] = $parameters;

tests/Validation/ValidationValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,6 +7158,28 @@ public function testArrayKeysValidationFailsWithNotAnArray()
71587158
);
71597159
}
71607160

7161+
public function testArrayKeysWithDotIntegerMin()
7162+
{
7163+
$trans = $this->getIlluminateArrayTranslator();
7164+
7165+
$data = [
7166+
'foo.bar' => -1,
7167+
];
7168+
7169+
$rules = [
7170+
'foo\.bar' => 'integer|min:1',
7171+
];
7172+
7173+
$expectedResult = [
7174+
'foo.bar' => [
7175+
'validation.min.numeric',
7176+
],
7177+
];
7178+
7179+
$validator = new Validator($trans, $data, $rules, [], []);
7180+
$this->assertEquals($expectedResult, $validator->getMessageBag()->getMessages());
7181+
}
7182+
71617183
protected function getTranslator()
71627184
{
71637185
return m::mock(TranslatorContract::class);

0 commit comments

Comments
 (0)