Skip to content

Commit 535c874

Browse files
committed
[Validator] Unique should support objects fields
1 parent 4c6049e commit 535c874

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\PropertyAccess\PropertyAccess;
15+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1416
use Symfony\Component\Validator\Constraint;
1517
use Symfony\Component\Validator\ConstraintValidator;
1618
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -21,6 +23,13 @@
2123
*/
2224
class UniqueValidator extends ConstraintValidator
2325
{
26+
private ?PropertyAccessorInterface $propertyAccessor;
27+
28+
public function __construct(PropertyAccessorInterface $propertyAccessor = null)
29+
{
30+
$this->propertyAccessor = $propertyAccessor;
31+
}
32+
2433
public function validate(mixed $value, Constraint $constraint)
2534
{
2635
if (!$constraint instanceof Unique) {
@@ -69,18 +78,34 @@ private function getNormalizer(Unique $unique): callable
6978
return $unique->normalizer;
7079
}
7180

72-
private function reduceElementKeys(array $fields, array $element): array
81+
private function reduceElementKeys(array $fields, array|object $element): array
7382
{
7483
$output = [];
7584
foreach ($fields as $field) {
7685
if (!\is_string($field)) {
7786
throw new UnexpectedTypeException($field, 'string');
7887
}
79-
if (isset($element[$field])) {
80-
$output[$field] = $element[$field];
88+
89+
// For no BC, because PropertyAccessor require brackets for array keys
90+
// Previous implementation, only check in array
91+
if (\is_array($element) && !str_contains($field, '[')) {
92+
$field = "[{$field}]";
93+
}
94+
95+
if (null !== $value = $this->getPropertyAccessor()->getValue($element, $field)) {
96+
$output[$field] = $value;
8197
}
8298
}
8399

84100
return $output;
85101
}
102+
103+
private function getPropertyAccessor(): PropertyAccessorInterface
104+
{
105+
if (null === $this->propertyAccessor) {
106+
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
107+
}
108+
109+
return $this->propertyAccessor;
110+
}
86111
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ public function getInvalidCollectionValues(): array
280280
['id' => 1, 'email' => 'bar@email.com'],
281281
['id' => 1, 'email' => 'foo@email.com'],
282282
], ['id']],
283+
'unique string attribute' => [[
284+
(object) ['lang' => 'eng', 'translation' => 'hi'],
285+
(object) ['lang' => 'eng', 'translation' => 'hello'],
286+
], ['lang']],
287+
'unique float attribute' => [[
288+
(object) ['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'],
289+
(object) ['latitude' => 52.520008, 'longitude' => 13.404954],
290+
(object) ['latitude' => 51.509865, 'longitude' => -0.118092],
291+
], ['latitude', 'longitude']],
292+
'unique int attribute' => [[
293+
(object) ['id' => 1, 'email' => 'bar@email.com'],
294+
(object) ['id' => 1, 'email' => 'foo@email.com'],
295+
], ['id']],
283296
];
284297
}
285298
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy