Skip to content

Commit

Permalink
bug #36216 [Validator] Assert Valid with many groups (phucwan91)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] Assert Valid with many groups

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36157
| License       | MIT

Make a reference object get validated by each group when using the Valid constraint with many groups

Commits
-------

c9aa3a8 bug #36157 [Validator] Assert Valid with many groups
  • Loading branch information
xabbuh committed Mar 28, 2020
2 parents a61101c + c9aa3a8 commit 0469be9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Symfony/Component/Validator/Tests/Validator/AbstractTest.php
Expand Up @@ -701,4 +701,25 @@ public function testNestedObjectIsValidatedIfGroupInValidConstraintIsValidated()

$this->assertCount(2, $violations);
}

public function testNestedObjectIsValidatedInMultipleGroupsIfGroupInValidConstraintIsValidated()
{
$entity = new Entity();
$entity->firstName = null;

$reference = new Reference();
$reference->value = null;

$entity->childA = $reference;

$this->metadata->addPropertyConstraint('firstName', new NotBlank());
$this->metadata->addPropertyConstraint('childA', new Valid(['groups' => ['group1', 'group2']]));

$this->referenceMetadata->addPropertyConstraint('value', new NotBlank(['groups' => 'group1']));
$this->referenceMetadata->addPropertyConstraint('value', new NotNull(['groups' => 'group2']));

$violations = $this->validator->validate($entity, null, ['Default', 'group1', 'group2']);

$this->assertCount(3, $violations);
}
}
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
Expand Down Expand Up @@ -782,8 +783,9 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
// that constraints belong to multiple validated groups
if (null !== $cacheKey) {
$constraintHash = spl_object_hash($constraint);

if ($constraint instanceof Composite) {
// instanceof Valid: In case of using a Valid constraint with many groups
// it makes a reference object get validated by each group
if ($constraint instanceof Composite || $constraint instanceof Valid) {
$constraintHash .= $group;
}

Expand Down

0 comments on commit 0469be9

Please sign in to comment.