Skip to content

Commit

Permalink
Add strict option to distinct attribute validation (#36669)
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertorussi authored Mar 19, 2021
1 parent 0de4d79 commit 72ea328
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public function validateDistinct($attribute, $value, $parameters)
return empty(preg_grep('/^'.preg_quote($value, '/').'$/iu', $data));
}

return ! in_array($value, array_values($data));
return ! in_array($value, array_values($data), in_array('strict', $parameters));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2395,6 +2395,15 @@ public function testValidateDistinct()
$v->messages()->setFormat(':message');
$this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
$this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));

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

$v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct:strict']);
$this->assertTrue($v->passes());
}

public function testValidateDistinctForTopLevelArrays()
Expand Down

0 comments on commit 72ea328

Please sign in to comment.