Skip to content

Commit f070965

Browse files
committed
added friendly exception when validator class does not exist
1 parent 904279e commit f070965

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,19 @@ public function testGetInstanceReturnsService()
6262
$factory = new ConstraintValidatorFactory($container, array('validator_constraint_alias' => 'validator_constraint_service'));
6363
$this->assertSame($validator, $factory->getInstance($constraint));
6464
}
65+
66+
/**
67+
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
68+
*/
69+
public function testGetInstanceInvalidValidatorClass()
70+
{
71+
$constraint = $this->getMock('Symfony\\Component\\Validator\\Constraint');
72+
$constraint
73+
->expects($this->once())
74+
->method('validatedBy')
75+
->will($this->returnValue('Fully\\Qualified\\ConstraintValidator\\Class\\Name'));
76+
77+
$factory = new ConstraintValidatorFactory(new Container());
78+
$factory->getInstance($constraint);
79+
}
6580
}

src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1717
use Symfony\Component\Validator\ConstraintValidatorInterface;
18+
use Symfony\Component\Validator\Exception\ValidatorException;
1819
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1920

2021
/**
@@ -61,13 +62,18 @@ public function __construct(ContainerInterface $container, array $validators = a
6162
*
6263
* @return ConstraintValidatorInterface A validator for the supplied constraint
6364
*
65+
* @throws ValidatorException When the validator class does not exist
6466
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
6567
*/
6668
public function getInstance(Constraint $constraint)
6769
{
6870
$name = $constraint->validatedBy();
6971

7072
if (!isset($this->validators[$name])) {
73+
if (!class_exists($name)) {
74+
throw new ValidatorException(sprintf('Constraint validator class "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s"', $name, get_class($constraint)));
75+
}
76+
7177
$this->validators[$name] = new $name();
7278
} elseif (is_string($this->validators[$name])) {
7379
$this->validators[$name] = $this->container->get($this->validators[$name]);

0 commit comments

Comments
 (0)