Skip to content

Commit c9aa3a8

Browse files
committed
bug #36157 [Validator] Assert Valid with many groups
1 parent a29ee7c commit c9aa3a8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,25 @@ public function testNestedObjectIsValidatedIfGroupInValidConstraintIsValidated()
701701

702702
$this->assertCount(2, $violations);
703703
}
704+
705+
public function testNestedObjectIsValidatedInMultipleGroupsIfGroupInValidConstraintIsValidated()
706+
{
707+
$entity = new Entity();
708+
$entity->firstName = null;
709+
710+
$reference = new Reference();
711+
$reference->value = null;
712+
713+
$entity->childA = $reference;
714+
715+
$this->metadata->addPropertyConstraint('firstName', new NotBlank());
716+
$this->metadata->addPropertyConstraint('childA', new Valid(['groups' => ['group1', 'group2']]));
717+
718+
$this->referenceMetadata->addPropertyConstraint('value', new NotBlank(['groups' => 'group1']));
719+
$this->referenceMetadata->addPropertyConstraint('value', new NotNull(['groups' => 'group2']));
720+
721+
$violations = $this->validator->validate($entity, null, ['Default', 'group1', 'group2']);
722+
723+
$this->assertCount(3, $violations);
724+
}
704725
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\Constraints\Composite;
1616
use Symfony\Component\Validator\Constraints\GroupSequence;
17+
use Symfony\Component\Validator\Constraints\Valid;
1718
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1819
use Symfony\Component\Validator\Context\ExecutionContext;
1920
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -782,8 +783,9 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
782783
// that constraints belong to multiple validated groups
783784
if (null !== $cacheKey) {
784785
$constraintHash = spl_object_hash($constraint);
785-
786-
if ($constraint instanceof Composite) {
786+
// instanceof Valid: In case of using a Valid constraint with many groups
787+
// it makes a reference object get validated by each group
788+
if ($constraint instanceof Composite || $constraint instanceof Valid) {
787789
$constraintHash .= $group;
788790
}
789791

0 commit comments

Comments
 (0)