Skip to content

Commit 7c3897b

Browse files
aprat84nicolas-grekas
authored andcommitted
[Validator] Fix type error for non-array items when Unique::fields is set
1 parent e60b532 commit 7c3897b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Constraints/UniqueValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validate(mixed $value, Constraint $constraint)
4545
foreach ($value as $element) {
4646
$element = $normalizer($element);
4747

48-
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
48+
if ($fields && !(\is_array($element) && $element = $this->reduceElementKeys($fields, $element))) {
4949
continue;
5050
}
5151

Tests/Constraints/UniqueValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ public static function getInvalidFieldNames(): array
243243
];
244244
}
245245

246+
/**
247+
* @dataProvider getValidCollectionValues
248+
*/
249+
public function testValidCollectionValues(array $value, array $fields)
250+
{
251+
$this->validator->validate($value, new Unique(fields: $fields));
252+
253+
$this->assertNoViolation();
254+
}
255+
256+
public static function getValidCollectionValues(): array
257+
{
258+
return [
259+
'unique empty item' => [
260+
[['field' => 1], ['field' => 2], []],
261+
['field'],
262+
],
263+
'unique non-array item' => [
264+
[['field' => 1], ['field' => 2], '', 1, null],
265+
['field'],
266+
],
267+
];
268+
}
269+
246270
/**
247271
* @dataProvider getInvalidCollectionValues
248272
*/

0 commit comments

Comments
 (0)