diff --git a/src/Symfony/Component/Validator/Constraints/Length.php b/src/Symfony/Component/Validator/Constraints/Length.php index d9b0d1f1c512..4a0157c1c9c2 100644 --- a/src/Symfony/Component/Validator/Constraints/Length.php +++ b/src/Symfony/Component/Validator/Constraints/Length.php @@ -57,13 +57,6 @@ public function __construct($options = null) parent::__construct($options); - if (null === $this->allowEmptyString) { - $this->allowEmptyString = true; - if (null !== $this->min) { - @trigger_error(sprintf('Using the "%s" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false.', self::class), E_USER_DEPRECATED); - } - } - if (null === $this->min && null === $this->max) { throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']); } diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php index b1b5d7c7700e..69a7c9671590 100644 --- a/src/Symfony/Component/Validator/Constraints/LengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -30,7 +30,11 @@ public function validate($value, Constraint $constraint) throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length'); } - if (null === $value || ('' === $value && $constraint->allowEmptyString)) { + if (null !== $constraint->min && null === $constraint->allowEmptyString) { + @trigger_error(sprintf('Using the "%s" constraint with the "min" option without setting the "allowEmptyString" one is deprecated and defaults to true. In 5.0, it will become optional and default to false.', Length::class), E_USER_DEPRECATED); + } + + if (null === $value || ('' === $value && ($constraint->allowEmptyString ?? true))) { return; } diff --git a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php index 139d252fc06b..43db82ecca0c 100644 --- a/src/Symfony/Component/Validator/Mapping/GenericMetadata.php +++ b/src/Symfony/Component/Validator/Mapping/GenericMetadata.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Validator\Mapping; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Constraints\Length; +use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Traverse; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -167,6 +169,8 @@ public function addConstraints(array $constraints) */ public function getConstraints() { + $this->configureLengthConstraints($this->constraints); + return $this->constraints; } @@ -187,9 +191,10 @@ public function hasConstraints() */ public function findConstraints($group) { - return isset($this->constraintsByGroup[$group]) - ? $this->constraintsByGroup[$group] - : []; + $constraints = $this->constraintsByGroup[$group] ?? []; + $this->configureLengthConstraints($constraints); + + return $constraints; } /** @@ -207,4 +212,26 @@ public function getTraversalStrategy() { return $this->traversalStrategy; } + + private function configureLengthConstraints(array $constraints): void + { + $allowEmptyString = true; + + foreach ($constraints as $constraint) { + if ($constraint instanceof NotBlank) { + $allowEmptyString = false; + break; + } + } + + if ($allowEmptyString) { + return; + } + + foreach ($constraints as $constraint) { + if ($constraint instanceof Length && null === $constraint->allowEmptyString) { + $constraint->allowEmptyString = false; + } + } + } }