Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,52 @@ Uid
Validator
---------

* Deprecate configuring constraint options implicitly with the XML format

*Before*

```xml
<class name="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
<constraint name="Callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</constraint>
</class>
```

*After*

```xml
<class name="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
<constraint name="Callback">
<option name="callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</option>
</constraint>
</class>
```
* Deprecate configuring constraint options implicitly with the YAML format

*Before*

```yaml
Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
constraints:
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
```

*After*

```yaml
Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
constraints:
- Callback:
callback: validateMeStatic
- Callback:
callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
```
* Deprecate implementing `__sleep/wakeup()` on `GenericMetadata` implementations; use `__(un)serialize()` instead
* Deprecate passing a list of choices to the first argument of the `Choice` constraint. Use the `choices` option instead
* Deprecate `getRequiredOptions()` and `getDefaultOption()` methods of the `All`, `AtLeastOneOf`, `CardScheme`, `Collection`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category:
properties:
id:
- Type: int
- Type:
type: int

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\SubCategory:
properties:
id:
- Type: int
- Type:
type: int
47 changes: 46 additions & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ CHANGELOG
7.4
---

* Deprecate configuring constraint options implicitly with the XML format

Before:

```xml
<class name="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
<constraint name="Callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</constraint>
</class>
```

After:

```xml
<class name="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
<constraint name="Callback">
<option name="callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</option>
</constraint>
</class>
```
* Deprecate configuring constraint options implicitly with the YAML format

Before:

```yaml
Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
constraints:
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
```

After:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before /after are inverted, isn't it?


```yaml
Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
constraints:
- Callback:
callback: validateMeStatic
- Callback:
callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
```
* Add `#[ExtendsValidationFor]` to declare new constraints for a class
* Add `ValidatorBuilder::addAttributeMappings()` and `AttributeMetadataPass` to declare compile-time constraint metadata using attributes
* Add the `Video` constraint for validating video files
Expand Down Expand Up @@ -48,7 +94,6 @@ CHANGELOG
}
}
```

* Deprecate the `getRequiredOptions()` method of the base `Constraint` class. Use mandatory constructor arguments instead.

Before:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,25 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
}

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

return new $className($options['value']);
}

if (array_is_list($options)) {
trigger_deprecation('symfony/validator', '7.4', 'Configuring the "%s" without passing its option names is deprecated.', $className);

return new $className($options);
}

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

return new $className($options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
foreach ($nodes as $node) {
if (\count($node) > 0) {
if (\count($node->value) > 0) {
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']);

$options = [
'value' => $this->parseValues($node->value),
];
Expand All @@ -100,7 +102,7 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
$options['groups'] = (array) $options['groups'];
}

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

return $constraints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ protected function parseNodes(array $nodes): array
}

if (null !== $options && (!\is_array($options) || array_is_list($options))) {
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));

$options = [
'value' => $options,
];
}

$values[] = $this->newConstraint(key($childNodes), $options);
$values[] = $this->newConstraint(key($childNodes), $options, true);
} else {
if (\is_array($childNodes)) {
$childNodes = $this->parseNodes($childNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupProvider;
use Symfony\Component\Validator\Tests\Fixtures\Attribute\GroupProviderDto;
use Symfony\Component\Validator\Tests\Fixtures\CallbackClass;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument;
Expand Down Expand Up @@ -104,6 +105,7 @@ public function testLoadClassMetadataValueOption()
$loader->loadClassMetadata($metadata);

$expected = new ClassMetadata(Entity::class);
$expected->addConstraint(new Callback([CallbackClass::class, 'callback']));
$expected->addPropertyConstraint('firstName', new Type(type: 'string'));
$expected->addPropertyConstraint('firstName', new Choice(choices: ['A', 'B']));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupProvider;
use Symfony\Component\Validator\Tests\Fixtures\Attribute\GroupProviderDto;
use Symfony\Component\Validator\Tests\Fixtures\CallbackClass;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithRequiredArgument;
Expand Down Expand Up @@ -146,6 +147,8 @@ public function testLoadClassMetadataValueOption()
$loader->loadClassMetadata($metadata);

$expected = new ClassMetadata(Entity::class);
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback([CallbackClass::class, 'callback']));
$expected->addPropertyConstraint('firstName', new Type(type: 'string'));
$expected->addPropertyConstraint('firstName', new Choice(choices: ['A', 'B']));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>

<class name="Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity">
<constraint name="Callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</constraint>

<!-- PROPERTY CONSTRAINTS -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\

Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
constraints:
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
properties:
firstName:
# Constraint with single value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
<constraint name="Callback">validateMeStatic</constraint>

<constraint name="Callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
<option name="callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</option>
</constraint>

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

<!-- Constraint with named arguments support with array value -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
<value>foo</value>
<value>bar</value>
<option name="choices">
<value>foo</value>
<value>bar</value>
</option>
</constraint>

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

<!-- Constraint with child constraints -->
<constraint name="All">
<constraint name="NotNull" />
<constraint name="Range">
<option name="min">3</option>
</constraint>

<option name="constraints">
<constraint name="NotNull" />
<constraint name="Range">
<option name="min">3</option>
</constraint>
</option>
</constraint>

<!-- Option with child constraints -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
# Custom constraint with namespaces prefix
- "custom:ConstraintB": ~
# Callbacks
- Callback: validateMe
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
- Callback:
callback: validateMe
- Callback:
callback: validateMeStatic
- Callback:
callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
# Constraint with named arguments support without value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments: ~
# Constraint with named arguments support with scalar value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: foo
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments:
choices: foo
# Constraint with named arguments support with array value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: [foo, bar]
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments:
choices: [foo, bar]

properties:
firstName:
# Constraint without value
- NotNull: ~
# Constraint with child constraints
- All:
- NotNull: ~
- Range:
min: 3
constraints:
- NotNull: ~
- Range:
min: 3
# Option with child constraints
- All:
constraints:
Expand Down
Loading