Skip to content

Commit

Permalink
Merge pull request #29499 from me-shaon/fix-distinct-validation
Browse files Browse the repository at this point in the history
[5.8] Fix top level wildcard validation for 'distinct'
  • Loading branch information
taylorotwell authored Aug 11, 2019
2 parents 93cf816 + 9d5ce0e commit 9352f00
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function explodeWildcardRules($results, $attribute, $rules)
foreach ($data as $key => $value) {
if (Str::startsWith($key, $attribute) || (bool) preg_match('/^'.$pattern.'\z/', $key)) {
foreach ((array) $rules as $rule) {
$this->implicitAttributes[$attribute][] = $key;
$this->implicitAttributes[$attribute][] = strval($key);

$results = $this->mergeRules($results, $key, $rule);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,29 @@ public function testValidateDistinct()
$this->assertEquals('There is a duplication!', $v->messages()->first('foo.1'));
}

public function testValidateDistinctForTopLevelArrays()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct']);
$this->assertFalse($v->passes());

$v = new Validator($trans, [['foo' => 1], ['foo' => 1]], ['*' => 'array', '*.foo' => 'distinct']);
$this->assertFalse($v->passes());

$v = new Validator($trans, [['foo' => 'a'], ['foo' => 'A']], ['*' => 'array', '*.foo' => 'distinct:ignore_case']);
$this->assertFalse($v->passes());

$v = new Validator($trans, [['foo' => [['id' => 1]]], ['foo' => [['id' => 1]]]], ['*' => 'array', '*.foo' => 'array', '*.foo.*.id' => 'distinct']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo', 'foo'], ['*' => 'distinct'], ['*.distinct' => 'There is a duplication!']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('There is a duplication!', $v->messages()->first('0'));
$this->assertEquals('There is a duplication!', $v->messages()->first('1'));
}

public function testValidateUnique()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 9352f00

Please sign in to comment.