Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d444bed

Browse filesBrowse files
committed
added friendly exception when validator class does not exist
1 parent 904279e commit d444bed
Copy full SHA for d444bed

File tree

2 files changed

+22
-0
lines changed
Filter options

2 files changed

+22
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Validator/ConstraintValidatorFactoryTest.php
+15Lines changed: 15 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php
+7Lines changed: 7 additions & 0 deletions
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,19 @@ 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(
75+
'Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s"', $name, get_class($constraint)));
76+
}
77+
7178
$this->validators[$name] = new $name();
7279
} elseif (is_string($this->validators[$name])) {
7380
$this->validators[$name] = $this->container->get($this->validators[$name]);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.