bug #24983 [Validator] enter the context in which to validate (xabbuh)
This PR was merged into the 3.4 branch.
Discussion
----------
[Validator] enter the context in which to validate
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #24246
| License | MIT
| Doc PR |
Commits
-------
7359cbe063
[Validator] enter the context in which to validate
This commit is contained in:
commit
2f19ddbd47
@ -26,12 +26,9 @@ class ValidValidator extends ConstraintValidator
|
|||||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
|
||||||
}
|
}
|
||||||
|
|
||||||
$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));
|
$this->context
|
||||||
|
->getValidator()
|
||||||
foreach ($violations as $violation) {
|
->inContext($this->context)
|
||||||
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
|
->validate($value, null, array($this->context->getGroup()));
|
||||||
->atPath($violation->getPropertyPath())
|
|
||||||
->addViolation();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Component\Validator\Tests\Constraints;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
use Symfony\Component\Validator\Constraints\ValidValidator;
|
||||||
|
use Symfony\Component\Validator\ValidatorBuilder;
|
||||||
|
|
||||||
|
class ValidValidatorTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testPropertyPathsArePassedToNestedContexts()
|
||||||
|
{
|
||||||
|
$validatorBuilder = new ValidatorBuilder();
|
||||||
|
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
|
||||||
|
|
||||||
|
$violations = $validator->validate(new Foo(), null, array('nested'));
|
||||||
|
|
||||||
|
$this->assertCount(1, $violations);
|
||||||
|
$this->assertSame('fooBar.fooBarBaz.foo', $violations->get(0)->getPropertyPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createValidator()
|
||||||
|
{
|
||||||
|
return new ValidValidator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Assert\Valid(groups={"nested"})
|
||||||
|
*/
|
||||||
|
public $fooBar;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->fooBar = new FooBar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FooBar
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Assert\Valid(groups={"nested"})
|
||||||
|
*/
|
||||||
|
public $fooBarBaz;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->fooBarBaz = new FooBarBaz();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FooBarBaz
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Assert\NotBlank(groups={"nested"})
|
||||||
|
*/
|
||||||
|
public $foo;
|
||||||
|
}
|
Reference in New Issue
Block a user