Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fcfb1d6

Browse filesBrowse files
committed
bug #36157 [Validator] Assert Valid with many groups
1 parent a29ee7c commit fcfb1d6
Copy full SHA for fcfb1d6

File tree

2 files changed

+25
-2
lines changed
Filter options

2 files changed

+25
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Validator/AbstractTest.php
+21Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php
+4-2Lines 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 (issue #36157)
788+
if ($constraint instanceof Composite || $constraint instanceof Valid) {
787789
$constraintHash .= $group;
788790
}
789791

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.