Skip to content

Commit 72ea328

Browse files
Add strict option to distinct attribute validation (#36669)
1 parent 0de4d79 commit 72ea328

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ public function validateDistinct($attribute, $value, $parameters)
556556
return empty(preg_grep('/^'.preg_quote($value, '/').'$/iu', $data));
557557
}
558558

559-
return ! in_array($value, array_values($data));
559+
return ! in_array($value, array_values($data), in_array('strict', $parameters));
560560
}
561561

562562
/**

tests/Validation/ValidationValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,15 @@ public function testValidateDistinct()
23952395
$v->messages()->setFormat(':message');
23962396
$this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
23972397
$this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
2398+
2399+
$v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct'], ['foo.*.distinct' => 'There is a duplication!']);
2400+
$this->assertFalse($v->passes());
2401+
$v->messages()->setFormat(':message');
2402+
$this->assertSame('There is a duplication!', $v->messages()->first('foo.0'));
2403+
$this->assertSame('There is a duplication!', $v->messages()->first('foo.1'));
2404+
2405+
$v = new Validator($trans, ['foo' => ['0100', '100']], ['foo.*' => 'distinct:strict']);
2406+
$this->assertTrue($v->passes());
23982407
}
23992408

24002409
public function testValidateDistinctForTopLevelArrays()

0 commit comments

Comments
 (0)