Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit53ed983

Browse files
committed
deprecate implicit constraint option names in YAML/XML mapping files
1 parent120b8cc commit53ed983

File tree

9 files changed

+76
-21
lines changed

9 files changed

+76
-21
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category:
22
properties:
33
id:
4-
-Type:int
4+
-Type:
5+
type:int
56

67
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\SubCategory:
78
properties:
89
id:
9-
-Type:int
10+
-Type:
11+
type:int

‎src/Symfony/Component/Validator/CHANGELOG.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ CHANGELOG
44
7.4
55
---
66

7+
* Deprecate configuring constraint options implicitly with the XML format
8+
9+
Before:
10+
11+
```xml
12+
<classname="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
13+
<constraintname="Callback">
14+
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
15+
<value>callback</value>
16+
</constraint>
17+
</class>
18+
```
19+
20+
After:
21+
22+
```xml
23+
<classname="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
24+
<optionname="callback">
25+
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
26+
<value>callback</value>
27+
</option>
28+
</class>
29+
```
30+
731
* Add`#[ExtendsValidationFor]` to declare new constraints for a class
832
* Add`ValidatorBuilder::addAttributeMappings()` and`AttributeMetadataPass` to declare compile-time constraint metadata using attributes
933
* Add the`Video` constraint for validating video files

‎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
returnnew$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
returnnew$className($options);
102108
}
103109

104110
try {
105111
returnnew$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
returnnew$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 ($nodesas$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/XmlFileLoaderTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
useSymfony\Component\Validator\Mapping\Loader\XmlFileLoader;
3131
useSymfony\Component\Validator\Tests\Dummy\DummyGroupProvider;
3232
useSymfony\Component\Validator\Tests\Fixtures\Attribute\GroupProviderDto;
33+
useSymfony\Component\Validator\Tests\Fixtures\CallbackClass;
3334
useSymfony\Component\Validator\Tests\Fixtures\ConstraintA;
3435
useSymfony\Component\Validator\Tests\Fixtures\ConstraintB;
3536
useSymfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument;
@@ -104,6 +105,7 @@ public function testLoadClassMetadataValueOption()
104105
$loader->loadClassMetadata($metadata);
105106

106107
$expected =newClassMetadata(Entity::class);
108+
$expected->addConstraint(newCallback([CallbackClass::class,'callback']));
107109
$expected->addPropertyConstraint('firstName',newType(type:'string'));
108110
$expected->addPropertyConstraint('firstName',newChoice(choices: ['A','B']));
109111

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<namespaceprefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>
88

99
<classname="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
10+
<constraintname="Callback">
11+
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
12+
<value>callback</value>
13+
</constraint>
1014

1115
<!-- PROPERTY CONSTRAINTS-->
1216

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

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

2929
<constraintname="Callback">
30-
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
31-
<value>callback</value>
30+
<optionname="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
<constraintname="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
46-
<value>foo</value>
47-
<value>bar</value>
48+
<optionname="choices">
49+
<value>foo</value>
50+
<value>bar</value>
51+
</option>
4852
</constraint>
4953

5054
<!-- Constraint with named arguments support with exactly one group-->
@@ -61,11 +65,12 @@
6165

6266
<!-- Constraint with child constraints-->
6367
<constraintname="All">
64-
<constraintname="NotNull" />
65-
<constraintname="Range">
66-
<optionname="min">3</option>
67-
</constraint>
68-
68+
<optionname="constraints">
69+
<constraintname="NotNull" />
70+
<constraintname="Range">
71+
<optionname="min">3</option>
72+
</constraint>
73+
</option>
6974
</constraint>
7075

7176
<!-- Option with child constraints-->

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

[8]ページ先頭

©2009-2025 Movatter.jp