diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 15e0801fb9..cd4f967b4e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -718,6 +718,7 @@ class FrameworkExtension extends Extension switch ($config['api']) { case '2.4': $api = Validation::API_VERSION_2_4; + $container->setParameter('validator.validator_factory.class', $container->getParameter('validator.legacy_validator_factory.class')); break; case '2.5': $api = Validation::API_VERSION_2_5; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml index 336379a11b..e0cd37d6b1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.xml @@ -11,6 +11,7 @@ Symfony\Component\Validator\Mapping\Cache\ApcCache Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory + Symfony\Bundle\FrameworkBundle\Validator\LegacyConstraintValidatorFactory Symfony\Component\Validator\Constraints\ExpressionValidator Symfony\Component\Validator\Constraints\EmailValidator diff --git a/src/Symfony/Bundle/FrameworkBundle/Validator/LegacyConstraintValidatorFactory.php b/src/Symfony/Bundle/FrameworkBundle/Validator/LegacyConstraintValidatorFactory.php new file mode 100644 index 0000000000..ac85eb4fb5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Validator/LegacyConstraintValidatorFactory.php @@ -0,0 +1,91 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Validator; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidatorFactoryInterface; +use Symfony\Component\Validator\ConstraintValidatorInterface; +use Symfony\Component\Validator\Exception\UnexpectedTypeException; + +/** + * Like {@link ConstraintValidatorFactory}, but aware of services compatible + * with the 2.4 API. + * + * @author Bernhard Schussek + * @author Kris Wallsmith + * + * @see ConstraintValidatorFactory + */ +class LegacyConstraintValidatorFactory implements ConstraintValidatorFactoryInterface +{ + const BASE_NAMESPACE = 'Symfony\\Component\\Validator\\Constraints'; + + protected $container; + protected $validators; + + /** + * Constructor. + * + * @param ContainerInterface $container The service container + * @param array $validators An array of validators + */ + public function __construct(ContainerInterface $container, array $validators = array()) + { + $this->container = $container; + $this->validators = $validators; + } + + /** + * Returns the validator for the supplied constraint. + * + * @param Constraint $constraint A constraint + * + * @return ConstraintValidatorInterface A validator for the supplied constraint + * + * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface + */ + public function getInstance(Constraint $constraint) + { + $name = $constraint->validatedBy(); + + if (!isset($this->validators[$name])) { + switch (get_class($constraint)) { + case self::BASE_NAMESPACE.'\\All': + $name = self::BASE_NAMESPACE.'\\LegacyAllValidator'; + break; + case self::BASE_NAMESPACE.'\\Choice': + $name = self::BASE_NAMESPACE.'\\LegacyChoiceValidator'; + break; + case self::BASE_NAMESPACE.'\\Collection': + $name = self::BASE_NAMESPACE.'\\LegacyCollectionValidator'; + break; + case self::BASE_NAMESPACE.'\\Count': + $name = self::BASE_NAMESPACE.'\\LegacyCountValidator'; + break; + case self::BASE_NAMESPACE.'\\Length': + $name = self::BASE_NAMESPACE.'\\LegacyLengthValidator'; + break; + } + + $this->validators[$name] = new $name(); + } elseif (is_string($this->validators[$name])) { + $this->validators[$name] = $this->container->get($this->validators[$name]); + } + + if (!$this->validators[$name] instanceof ConstraintValidatorInterface) { + throw new UnexpectedTypeException($this->validators[$name], 'Symfony\Component\Validator\ConstraintValidatorInterface'); + } + + return $this->validators[$name]; + } +}