Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 77df90b

Browse filesBrowse files
Brajk19fabpot
authored andcommitted
[Validator] UniqueValidator - normalize before reducing keys
1 parent b85a083 commit 77df90b
Copy full SHA for 77df90b

File tree

2 files changed

+33
-2
lines changed
Filter options

2 files changed

+33
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/UniqueValidator.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function validate(mixed $value, Constraint $constraint)
4343
$collectionElements = [];
4444
$normalizer = $this->getNormalizer($constraint);
4545
foreach ($value as $element) {
46+
$element = $normalizer($element);
47+
4648
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
4749
continue;
4850
}
4951

50-
$element = $normalizer($element);
51-
5252
if (\in_array($element, $collectionElements, true)) {
5353
$this->context->buildViolation($constraint->message)
5454
->setParameter('{{ value }}', $this->formatValue($value))

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1717
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1818
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19+
use Symfony\Component\Validator\Tests\Dummy\DummyClassOne;
1920

2021
class UniqueValidatorTest extends ConstraintValidatorTestCase
2122
{
@@ -283,6 +284,36 @@ public static function getInvalidCollectionValues(): array
283284
],
284285
];
285286
}
287+
288+
public function testArrayOfObjectsUnique()
289+
{
290+
$array = [
291+
new DummyClassOne(),
292+
new DummyClassOne(),
293+
new DummyClassOne(),
294+
];
295+
296+
$array[0]->code = '1';
297+
$array[1]->code = '2';
298+
$array[2]->code = '3';
299+
300+
$this->validator->validate(
301+
$array,
302+
new Unique(
303+
normalizer: [self::class, 'normalizeDummyClassOne'],
304+
fields: 'code'
305+
)
306+
);
307+
308+
$this->assertNoViolation();
309+
}
310+
311+
public static function normalizeDummyClassOne(DummyClassOne $obj): array
312+
{
313+
return [
314+
'code' => $obj->code,
315+
];
316+
}
286317
}
287318

288319
class CallableClass

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.