Skip to content

Commit fe30393

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: [Workflow] Fix Marking when it must contains more than one tokens [Validator] UniqueValidator - normalize before reducing keys backport NoSuspiciousCharactersValidator test Fix merge
2 parents 6a73d47 + e65b8b1 commit fe30393

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Constraints/UniqueValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function validate(mixed $value, Constraint $constraint): void
4040
$collectionElements = [];
4141
$normalizer = $this->getNormalizer($constraint);
4242
foreach ($value as $element) {
43+
$element = $normalizer($element);
44+
4345
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
4446
continue;
4547
}
4648

47-
$element = $normalizer($element);
48-
4949
if (\in_array($element, $collectionElements, true)) {
5050
$this->context->buildViolation($constraint->message)
5151
->setParameter('{{ value }}', $this->formatValue($value))

Tests/Constraints/NoSuspiciousCharactersValidatorTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,17 @@ public function testSuspiciousStrings(string $string, array $options, array $err
6060
{
6161
$this->validator->validate($string, new NoSuspiciousCharacters($options));
6262

63-
$violations = $this->buildViolation(reset($errors))
64-
->setCode(key($errors))
65-
->setParameter('{{ value }}', '"'.$string.'"')
66-
;
67-
68-
while ($message = next($errors)) {
69-
$violations = $violations->buildNextViolation($message)
70-
->setCode(key($errors))
63+
$violations = null;
64+
65+
foreach ($errors as $code => $message) {
66+
if (null === $violations) {
67+
$violations = $this->buildViolation($message);
68+
} else {
69+
$violations = $violations->buildNextViolation($message);
70+
}
71+
72+
$violations = $violations
73+
->setCode($code)
7174
->setParameter('{{ value }}', '"'.$string.'"')
7275
;
7376
}

Tests/Constraints/UniqueValidatorTest.php

Lines 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)