8000 [Validator] Fixed grouped composite constraints · symfony/symfony@b53d911 · GitHub
[go: up one dir, main page]

Skip to content

Commit b53d911

Browse files
committed
[Validator] Fixed grouped composite constraints
1 parent 9e84e0f commit b53d911

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Symfony\Component\Validator\Tests\Validator;
1313

1414
use Symfony\Component\Translation\IdentityTranslator;
15+
use Symfony\Component\Validator\Constraints\All;
16+
use Symfony\Component\Validator\Constraints\Collection;
17+
use Symfony\Component\Validator\Constraints\Length;
18+
use Symfony\Component\Validator\Constraints\NotBlank;
1519
use Symfony\Component\Validator\ConstraintValidatorFactory;
1620
use Symfony\Component\Validator\Context\ExecutionContextFactory;
1721
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
@@ -95,4 +99,38 @@ public function testRelationBetweenChildAAndChildB()
9599

96100
$validator->validate($entity, null, array());
97101
}
102+
103+
public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
104+
{
105+
$this->metadata->addPropertyConstraint('data', new Collection(array('fields' => array(
106+
'one' => array(new NotBlank(array('groups' => 'one')), new Length(array('min' => 2, 'groups' => 'two'))),
107+
'two' => array(new NotBlank(array('groups' => 'two'))),
108+
))));
109+
110+
$entity = new Entity();
111+
$entity->data = array('one' => 't', 'two' => '');
112+
113+
$violations = $this->validator->validate($entity, null, array('one', 'two'));
114+
115+
$this->assertCount(2, $violations);
116+
$this->assertInstanceOf(Length::class, $violations->get(0)->getConstraint());
117+
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
118+
}
119+
120+
public function testAllConstraintValidateAllGroupsForNestedConstraints()
121+
{
122+
$this->metadata->addPropertyConstraint('data', new All(array('constraints' => array(
8000
123+
new NotBlank(array('groups' => 'one')),
124+
new Length(array('min' => 2, 'groups' => 'two')),
125+
))));
126+
127+
$entity = new Entity();
128+
$entity->data = array('one' => 't', 'two' => '');
129+
130+
$violations = $this->validator->validate($entity, null, array('one', 'two'));
131+
132+
$this->assertCount(2, $violations);
133+
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
134+
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
135+
}
98136
}

src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Validator;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\Composite;
1516
use Symfony\Component\Validator\Constraints\GroupSequence;
1617
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1718
use Symfony\Component\Validator\Context\ExecutionContext;
@@ -787,6 +788,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
787788
if (null !== $cacheKey) {
788789
$constraintHash = spl_object_hash($constraint);
789790

791+
if ($constraint instanceof Composite) {
792+
$constraintHash .= $group;
793+
}
794+
790795
if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
791796
continue;
792797
}

0 commit comments

Comments
 (0)
0