diff --git a/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php b/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php index 67b7b20101a7f..0910d8bf873d3 100644 --- a/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php +++ b/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php @@ -101,6 +101,15 @@ protected function newConstraint(string $name, mixed $options = null): Constrain return new $className($options); } + if (isset($options['value']) && \in_array($name, ['Length', 'Count'], true)) { + trigger_deprecation('symfony/validator', '7.3', 'Using the "value" option in configuration for the "%s" constraint is deprecated. Use "exactly" instead.', $name); + + if (!isset($options['exactly'])) { + $options['exactly'] = $options['value']; + unset($options['value']); + } + } + return new $className(...$options); } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php index 08a4bb862cbf1..7e1d0849cbba9 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -18,6 +18,7 @@ use Symfony\Component\Validator\Constraints\Choice; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\IsTrue; +use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Constraints\Range; use Symfony\Component\Validator\Constraints\Regex; @@ -188,4 +189,22 @@ public function testLoadConstraintWithoutNamedArgumentsSupport() $loader->loadClassMetadata($metadata); } + + /** + * @group legacy + */ + public function testLengthConstraintValueOptionTriggersDeprecation() + { + $this->expectDeprecation('Since symfony/validator 7.3: Using the "value" option in configuration for the "Length" constraint is deprecated. Use "exactly" instead.'); + + $loader = new XmlFileLoader(__DIR__.'/constraint-mapping-exactly-value.xml'); + $metadata = new ClassMetadata(Entity_81::class); + $loader->loadClassMetadata($metadata); + $constraints = $metadata->getPropertyMetadata('title')[0]->constraints; + + self::assertCount(1, $constraints); + self::assertInstanceOf(Length::class, $constraints[0]); + self::assertSame(6, $constraints[0]->min); + self::assertSame(6, $constraints[0]->max); + } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping-exactly-value.xml b/src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping-exactly-value.xml new file mode 100644 index 0000000000000..40e04f3a368b7 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Mapping/Loader/constraint-mapping-exactly-value.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + +