From b66ea5e16ebbc0e2da664e070309116bdcb808bf Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 12 Aug 2016 08:09:39 -0400 Subject: [PATCH] added friendly exception when constraint validator does not exist or it is not enabled --- .../Validator/ConstraintValidatorFactoryTest.php | 15 +++++++++++++++ .../Validator/ConstraintValidatorFactory.php | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php index b73c9f81fa..b61851bb0c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php @@ -62,4 +62,19 @@ class ConstraintValidatorFactoryTest extends \PHPUnit_Framework_TestCase $factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service')); $this->assertSame($validator, $factory->getInstance($constraint)); } + + /** + * @expectedException \Symfony\Component\Validator\Exception\ValidatorException + */ + public function testGetInstanceInvalidValidatorClass() + { + $constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint'); + $constraint + ->expects($this->once()) + ->method('validatedBy') + ->will($this->returnValue('Fully\\Qualified\\ConstraintValidator\\Class\\Name')); + + $factory = new ConstraintValidatorFactory(new Container()); + $factory->getInstance($constraint); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php b/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php index 66f0d012d3..4e36678212 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php +++ b/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php @@ -15,6 +15,7 @@ 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\ValidatorException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** @@ -61,6 +62,7 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface * * @return ConstraintValidatorInterface A validator for the supplied constraint * + * @throws ValidatorException When the validator class does not exist * @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface */ public function getInstance(Constraint $constraint) @@ -68,6 +70,10 @@ class ConstraintValidatorFactory implements ConstraintValidatorFactoryInterface $name = $constraint->validatedBy(); if (!isset($this->validators[$name])) { + if (!class_exists($name)) { + throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s".', $name, get_class($constraint))); + } + $this->validators[$name] = new $name(); } elseif (is_string($this->validators[$name])) { $this->validators[$name] = $this->container->get($this->validators[$name]);