[FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy validators
This commit is contained in:
parent
7504448ee7
commit
870a41a594
@ -718,6 +718,7 @@ class FrameworkExtension extends Extension
|
|||||||
switch ($config['api']) {
|
switch ($config['api']) {
|
||||||
case '2.4':
|
case '2.4':
|
||||||
$api = Validation::API_VERSION_2_4;
|
$api = Validation::API_VERSION_2_4;
|
||||||
|
$container->setParameter('validator.validator_factory.class', $container->getParameter('validator.legacy_validator_factory.class'));
|
||||||
break;
|
break;
|
||||||
case '2.5':
|
case '2.5':
|
||||||
$api = Validation::API_VERSION_2_5;
|
$api = Validation::API_VERSION_2_5;
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
<parameter key="validator.mapping.cache.apc.class">Symfony\Component\Validator\Mapping\Cache\ApcCache</parameter>
|
<parameter key="validator.mapping.cache.apc.class">Symfony\Component\Validator\Mapping\Cache\ApcCache</parameter>
|
||||||
<parameter key="validator.mapping.cache.prefix" />
|
<parameter key="validator.mapping.cache.prefix" />
|
||||||
<parameter key="validator.validator_factory.class">Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory</parameter>
|
<parameter key="validator.validator_factory.class">Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory</parameter>
|
||||||
|
<parameter key="validator.legacy_validator_factory.class">Symfony\Bundle\FrameworkBundle\Validator\LegacyConstraintValidatorFactory</parameter>
|
||||||
<parameter key="validator.expression.class">Symfony\Component\Validator\Constraints\ExpressionValidator</parameter>
|
<parameter key="validator.expression.class">Symfony\Component\Validator\Constraints\ExpressionValidator</parameter>
|
||||||
<parameter key="validator.email.class">Symfony\Component\Validator\Constraints\EmailValidator</parameter>
|
<parameter key="validator.email.class">Symfony\Component\Validator\Constraints\EmailValidator</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* 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 <bschussek@gmail.com>
|
||||||
|
* @author Kris Wallsmith <kris@symfony.com>
|
||||||
|
*
|
||||||
|
* @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];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user