Skip to content

Commit d15391f

Browse files
committed
deprecate implicit constraint option names in YAML/XML mapping files
1 parent 91f8966 commit d15391f

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,25 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
9494
}
9595

9696
if (1 === \count($options) && isset($options['value'])) {
97+
if (\func_num_args() < 3 || !func_get_arg(2)) {
98+
trigger_deprecation('symfony/validator', '7.4', 'Using the "value" option to configure the "%s" constraint is deprecated.', $className);
99+
}
100+
97101
return new $className($options['value']);
98102
}
99103

100104
if (array_is_list($options)) {
105+
trigger_deprecation('symfony/validator', '7.4', 'Configuring the "%s" without passing its option names is deprecated.', $className);
106+
101107
return new $className($options);
102108
}
103109

104110
try {
105111
return new $className(...$options);
106112
} catch (\Error $e) {
107113
if (str_starts_with($e->getMessage(), 'Unknown named parameter ')) {
114+
trigger_deprecation('symfony/validator', '7.4', 'Using option names not matching the named arguments of the "%s" constraint is deprecated.', $className);
115+
108116
return new $className($options);
109117
}
110118

src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
8080
foreach ($nodes as $node) {
8181
if (\count($node) > 0) {
8282
if (\count($node->value) > 0) {
83+
trigger_deprecation('symfony/validator', '7.4', 'Using the "value" XML element to configure an option for the "%s" is deprecated. Use the "option" element instead.', (string) $node['name']);
84+
8385
$options = [
8486
'value' => $this->parseValues($node->value),
8587
];
@@ -100,7 +102,7 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
100102
$options['groups'] = (array) $options['groups'];
101103
}
102104

103-
$constraints[] = $this->newConstraint((string) $node['name'], $options);
105+
$constraints[] = $this->newConstraint((string) $node['name'], $options, true);
104106
}
105107

106108
return $constraints;

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ protected function parseNodes(array $nodes): array
8787
}
8888

8989
if (null !== $options && (!\is_array($options) || array_is_list($options))) {
90+
trigger_deprecation('symfony/validator', '7.4', 'Not using a YAML mapping of constraint option names to their values to configure the "%s" constraint is deprecated.', key($childNodes));
91+
9092
$options = [
9193
'value' => $options,
9294
];
9395
}
9496

95-
$values[] = $this->newConstraint(key($childNodes), $options);
97+
$values[] = $this->newConstraint(key($childNodes), $options, true);
9698
} else {
9799
if (\is_array($childNodes)) {
98100
$childNodes = $this->parseNodes($childNodes);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
<constraint name="Callback">validateMeStatic</constraint>
2828

2929
<constraint name="Callback">
30-
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
31-
<value>callback</value>
30+
<option name="callback">
31+
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
32+
<value>callback</value>
33+
</option>
3234
</constraint>
3335

3436
<!-- Traverse with boolean default option -->
@@ -43,8 +45,10 @@
4345

4446
<!-- Constraint with named arguments support with array value -->
4547
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
46-
<value>foo</value>
47-
<value>bar</value>
48+
<option name="choices">
49+
<value>foo</value>
50+
<value>bar</value>
51+
</option>
4852
</constraint>
4953

5054
<!-- Constraint with named arguments support with exactly one group -->

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,31 @@ Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
1212
# Custom constraint with namespaces prefix
1313
- "custom:ConstraintB": ~
1414
# Callbacks
15-
- Callback: validateMe
16-
- Callback: validateMeStatic
17-
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
15+
- Callback:
16+
callback: validateMe
17+
- Callback:
18+
callback: validateMeStatic
19+
- Callback:
20+
callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
1821
# Constraint with named arguments support without value
1922
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments: ~
2023
# Constraint with named arguments support with scalar value
21-
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: foo
24+
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments:
25+
choices: foo
2226
# Constraint with named arguments support with array value
23-
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: [foo, bar]
27+
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments:
28+
choices: [foo, bar]
2429

2530
properties:
2631
firstName:
2732
# Constraint without value
2833
- NotNull: ~
2934
# Constraint with child constraints
3035
- All:
31-
- NotNull: ~
32-
- Range:
33-
min: 3
36+
constraints:
37+
- NotNull: ~
38+
- Range:
39+
min: 3
3440
# Option with child constraints
3541
- All:
3642
constraints:

0 commit comments

Comments
 (0)