Skip to content

Commit f553818

Browse files
bug #62716 [Validator] fix handling named arguments in Existence constraint (xabbuh)
This PR was merged into the 7.4 branch. Discussion ---------- [Validator] fix handling named arguments in `Existence` constraint | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62705 | License | MIT Commits ------- c495750 fix handling named arguments in Existence constraint
2 parents 887d6a6 + c495750 commit f553818

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Attribute\HasNamedArguments;
1415
use Symfony\Component\Validator\Constraint;
1516

1617
/**
@@ -20,6 +21,7 @@ abstract class Existence extends Composite
2021
{
2122
public array|Constraint $constraints = [];
2223

24+
#[HasNamedArguments]
2325
public function __construct(mixed $constraints = null, ?array $groups = null, mixed $payload = null)
2426
{
2527
if (!$constraints instanceof Constraint && !\is_array($constraints) || \is_array($constraints) && !array_is_list($constraints)) {

src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
use Symfony\Component\Validator\Constraints\Callback;
1919
use Symfony\Component\Validator\Constraints\Choice;
2020
use Symfony\Component\Validator\Constraints\Collection;
21+
use Symfony\Component\Validator\Constraints\Email;
2122
use Symfony\Component\Validator\Constraints\IsTrue;
2223
use Symfony\Component\Validator\Constraints\Length;
24+
use Symfony\Component\Validator\Constraints\NotBlank;
2325
use Symfony\Component\Validator\Constraints\NotNull;
26+
use Symfony\Component\Validator\Constraints\Optional;
2427
use Symfony\Component\Validator\Constraints\Range;
2528
use Symfony\Component\Validator\Constraints\Regex;
29+
use Symfony\Component\Validator\Constraints\Required;
2630
use Symfony\Component\Validator\Constraints\Traverse;
2731
use Symfony\Component\Validator\Constraints\Type;
2832
use Symfony\Component\Validator\Exception\MappingException;
@@ -83,6 +87,8 @@ public function testLoadClassMetadata()
8387
$expected->addPropertyConstraint('firstName', new Collection(fields: [
8488
'foo' => [new NotNull(), new Range(min: 3)],
8589
'bar' => [new Range(min: 5)],
90+
'baz' => new Required([new Email()]),
91+
'qux' => new Optional([new NotBlank()]),
8692
]));
8793
$expected->addPropertyConstraint('firstName', new Choice(
8894
message: 'Must be one of %choices%',

src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
use Symfony\Component\Validator\Constraints\Callback;
2020
use Symfony\Component\Validator\Constraints\Choice;
2121
use Symfony\Component\Validator\Constraints\Collection;
22+
use Symfony\Component\Validator\Constraints\Email;
2223
use Symfony\Component\Validator\Constraints\IsTrue;
24+
use Symfony\Component\Validator\Constraints\NotBlank;
2325
use Symfony\Component\Validator\Constraints\NotNull;
26+
use Symfony\Component\Validator\Constraints\Optional;
2427
use Symfony\Component\Validator\Constraints\Range;
28+
use Symfony\Component\Validator\Constraints\Required;
2529
use Symfony\Component\Validator\Constraints\Type;
2630
use Symfony\Component\Validator\Mapping\ClassMetadata;
2731
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
@@ -125,6 +129,8 @@ public function testLoadClassMetadata()
125129
$expected->addPropertyConstraint('firstName', new Collection(fields: [
126130
'foo' => [new NotNull(), new Range(min: 3)],
127131
'bar' => [new Range(min: 5)],
132+
'baz' => new Required([new Email()]),
133+
'qux' => new Optional([new NotBlank()]),
128134
]));
129135
$expected->addPropertyConstraint('firstName', new Choice(
130136
message: 'Must be one of %choices%',

src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@
9797
<option name="min">5</option>
9898
</constraint>
9999
</value>
100+
<value key="baz">
101+
<constraint name="Required">
102+
<option name="constraints">
103+
<constraint name="Email" />
104+
</option>
105+
</constraint>
106+
</value>
107+
<value key="qux">
108+
<constraint name="Optional">
109+
<option name="constraints">
110+
<constraint name="NotBlank" />
111+
</option>
112+
</constraint>
113+
</value>
100114
</option>
101115
</constraint>
102116

src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
5353
bar:
5454
- Range:
5555
min: 5
56+
baz:
57+
- Required:
58+
constraints:
59+
- Email: ~
60+
qux:
61+
- Optional:
62+
constraints:
63+
- NotBlank: ~
5664
# Constraint with options
5765
- Choice: { choices: [A, B], message: Must be one of %choices% }
5866
dummy:

0 commit comments

Comments
 (0)