Skip to content

Commit 7841134

Browse files
committed
[Validator] enter the context in which to validate
1 parent ecad1c4 commit 7841134

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/Symfony/Component/Validator/Constraints/ValidValidator.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Context\ExecutionContext;
1617
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1718

1819
/**
@@ -26,12 +27,9 @@ public function validate($value, Constraint $constraint)
2627
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
2728
}
2829

29-
$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
30-
31-
foreach ($violations as $violation) {
32-
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
33-
->atPath($violation->getPropertyPath())
34-
->addViolation();
35-
}
30+
$this->context
31+
->getValidator()
32+
->inContext($this->context)
33+
->validate($value, null, array($this->context->getGroup()));
3634
}
3735
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Constraints;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Validator\Constraints as Assert;
7+
use Symfony\Component\Validator\Constraints\ValidValidator;
8+
use Symfony\Component\Validator\ValidatorBuilder;
9+
10+
class ValidValidatorTest extends TestCase
11+
{
12+
public function testPropertyPathsArePassedToNestedContexts()
13+
{
14+
$validatorBuilder = new ValidatorBuilder();
15+
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
16+
17+
$violations = $validator->validate(new Foo(), null, array('nested'));
18+
19+
$this->assertCount(1, $violations);
20+
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
21+
}
22+
23+
protected function createValidator()
24+
{
25+
return new ValidValidator();
26+
}
27+
}
28+
29+
class Foo
30+
{
31+
/**
32+
* @Assert\Valid(groups={"nested"})
33+
*/
34+
public $fooBar;
35+
36+
public function __construct()
37+
{
38+
$this->fooBar = new FooBar();
39+
}
40+
}
41+
42+
class FooBar
43+
{
44+
/**
45+
* @Assert\Valid(groups={"nested"})
46+
*/
47+
public $fooBarBaz;
48+
49+
public function __construct()
50+
{
51+
$this->fooBarBaz = new FooBarBaz();
52+
}
53+
}
54+
55+
class FooBarBaz
56+
{
57+
/**
58+
* @Assert\NotBlank(groups={"nested"})
59+
*/
60+
public $foo;
61+
}

0 commit comments

Comments
 (0)