Skip to content

Commit 7359cbe

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

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ public function validate($value, Constraint $constraint)
2626
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
2727
}
2828

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-
}
29+
$this->context
30+
->getValidator()
31+
->inContext($this->context)
32+
->validate($value, null, array($this->context->getGroup()));
3633
}
3734
}
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)