Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Assert Valid with many groups #36216

Merged
merged 1 commit into from Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HeahDude @stof The proposed change is similar to #29499 that we discussed during SymfonyCon Lisbon. To me this looks good but I would like to get your opinion on this too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, looks good to me too.

$constraintHash .= $group;
}

Expand Down