Skip to content

[Validator] enter the context in which to validate #24983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/Symfony/Component/Validator/Constraints/ValidValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Valid');
}

$violations = $this->context->getValidator()->validate($value, null, array($this->context->getGroup()));

foreach ($violations as $violation) {
$this->context->buildViolation($violation->getMessage(), $violation->getParameters())
->atPath($violation->getPropertyPath())
->addViolation();
}
$this->context
->getValidator()
->inContext($this->context)
->validate($value, null, array($this->context->getGroup()));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}