[Validator] Fixed grouped composite constraints
This commit is contained in:
parent
9e84e0ff98
commit
b53d9112ca
@ -12,6 +12,10 @@
|
|||||||
namespace Symfony\Component\Validator\Tests\Validator;
|
namespace Symfony\Component\Validator\Tests\Validator;
|
||||||
|
|
||||||
use Symfony\Component\Translation\IdentityTranslator;
|
use Symfony\Component\Translation\IdentityTranslator;
|
||||||
|
use Symfony\Component\Validator\Constraints\All;
|
||||||
|
use Symfony\Component\Validator\Constraints\Collection;
|
||||||
|
use Symfony\Component\Validator\Constraints\Length;
|
||||||
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContextFactory;
|
use Symfony\Component\Validator\Context\ExecutionContextFactory;
|
||||||
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
|
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
|
||||||
@ -95,4 +99,38 @@ class RecursiveValidatorTest extends AbstractTest
|
|||||||
|
|
||||||
$validator->validate($entity, null, array());
|
$validator->validate($entity, null, array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
|
||||||
|
{
|
||||||
|
$this->metadata->addPropertyConstraint('data', new Collection(array('fields' => array(
|
||||||
|
'one' => array(new NotBlank(array('groups' => 'one')), new Length(array('min' => 2, 'groups' => 'two'))),
|
||||||
|
'two' => array(new NotBlank(array('groups' => 'two'))),
|
||||||
|
))));
|
||||||
|
|
||||||
|
$entity = new Entity();
|
||||||
|
$entity->data = array('one' => 't', 'two' => '');
|
||||||
|
|
||||||
|
$violations = $this->validator->validate($entity, null, array('one', 'two'));
|
||||||
|
|
||||||
|
$this->assertCount(2, $violations);
|
||||||
|
$this->assertInstanceOf(Length::class, $violations->get(0)->getConstraint());
|
||||||
|
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAllConstraintValidateAllGroupsForNestedConstraints()
|
||||||
|
{
|
||||||
|
$this->metadata->addPropertyConstraint('data', new All(array('constraints' => array(
|
||||||
|
new NotBlank(array('groups' => 'one')),
|
||||||
|
new Length(array('min' => 2, 'groups' => 'two')),
|
||||||
|
))));
|
||||||
|
|
||||||
|
$entity = new Entity();
|
||||||
|
$entity->data = array('one' => 't', 'two' => '');
|
||||||
|
|
||||||
|
$violations = $this->validator->validate($entity, null, array('one', 'two'));
|
||||||
|
|
||||||
|
$this->assertCount(2, $violations);
|
||||||
|
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
|
||||||
|
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Validator\Validator;
|
namespace Symfony\Component\Validator\Validator;
|
||||||
|
|
||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
use Symfony\Component\Validator\Constraints\Composite;
|
||||||
use Symfony\Component\Validator\Constraints\GroupSequence;
|
use Symfony\Component\Validator\Constraints\GroupSequence;
|
||||||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContext;
|
use Symfony\Component\Validator\Context\ExecutionContext;
|
||||||
@ -787,6 +788,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
if (null !== $cacheKey) {
|
if (null !== $cacheKey) {
|
||||||
$constraintHash = spl_object_hash($constraint);
|
$constraintHash = spl_object_hash($constraint);
|
||||||
|
|
||||||
|
if ($constraint instanceof Composite) {
|
||||||
|
$constraintHash .= $group;
|
||||||
|
}
|
||||||
|
|
||||||
if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
|
if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user