Skip to content

Commit f7158bd

Browse files
committed
[Validator] Fix type error for non-array items when Unique::fields is set
1 parent 9145a88 commit f7158bd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/Validator/Constraints/UniqueValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function validate(mixed $value, Constraint $constraint)
4343
$collectionElements = [];
4444
$normalizer = $this->getNormalizer($constraint);
4545
foreach ($value as $element) {
46-
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
46+
if ($fields && !(\is_array($element) && $element = $this->reduceElementKeys($fields, $element))) {
4747
continue;
4848
}
4949

src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php

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

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

0 commit comments

Comments
 (0)