Skip to content

Commit 9352f00

Browse files
authored
Merge pull request #29499 from me-shaon/fix-distinct-validation
[5.8] Fix top level wildcard validation for 'distinct'
2 parents 93cf816 + 9d5ce0e commit 9352f00

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Illuminate/Validation/ValidationRuleParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function explodeWildcardRules($results, $attribute, $rules)
131131
foreach ($data as $key => $value) {
132132
if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {
133133
foreach ((array) $rules as $rule) {
134-
$this->implicitAttributes[$attribute][] = $key;
134+
$this->implicitAttributes[$attribute][] = strval($key);
135135

136136
$results = $this->mergeRules($results, $key, $rule);
137137
}

tests/Validation/ValidationValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,29 @@ public function testValidateDistinct()
18991899
$this->assertEquals('There is a duplication!', $v->messages()->first('foo.1'));
19001900
}
19011901

1902+
public function testValidateDistinctForTopLevelArrays()
1903+
{
1904+
$trans = $this->getIlluminateArrayTranslator();
1905+
1906+
$v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct']);
1907+
$this->assertFalse($v->passes());
1908+
1909+
$v = new Validator($trans, [['foo' => 1], ['foo' => 1]], ['*' => 'array', '*.foo' => 'distinct']);
1910+
$this->assertFalse($v->passes());
1911+
1912+
$v = new Validator($trans, [['foo' => 'a'], ['foo' => 'A']], ['*' => 'array', '*.foo' => 'distinct:ignore_case']);
1913+
$this->assertFalse($v->passes());
1914+
1915+
$v = new Validator($trans, [['foo' => [['id' => 1]]], ['foo' => [['id' => 1]]]], ['*' => 'array', '*.foo' => 'array', '*.foo.*.id' => 'distinct']);
1916+
$this->assertFalse($v->passes());
1917+
1918+
$v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct'], ['*.distinct' => 'There is a duplication!']);
1919+
$this->assertFalse($v->passes());
1920+
$v->messages()->setFormat(':message');
1921+
$this->assertEquals('There is a duplication!', $v->messages()->first('0'));
1922+
$this->assertEquals('There is a duplication!', $v->messages()->first('1'));
1923+
}
1924+
19021925
public function testValidateUnique()
19031926
{
19041927
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)