bug #36157 [Validator] Assert Valid with many groups
This commit is contained in:
parent
a29ee7c220
commit
c9aa3a849a
@ -701,4 +701,25 @@ abstract class AbstractTest extends AbstractValidatorTest
|
|||||||
|
|
||||||
$this->assertCount(2, $violations);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\Validator\Validator;
|
|||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
use Symfony\Component\Validator\Constraints\Composite;
|
use Symfony\Component\Validator\Constraints\Composite;
|
||||||
use Symfony\Component\Validator\Constraints\GroupSequence;
|
use Symfony\Component\Validator\Constraints\GroupSequence;
|
||||||
|
use Symfony\Component\Validator\Constraints\Valid;
|
||||||
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContext;
|
use Symfony\Component\Validator\Context\ExecutionContext;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||||
@ -782,8 +783,9 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
|
|||||||
// that constraints belong to multiple validated groups
|
// that constraints belong to multiple validated groups
|
||||||
if (null !== $cacheKey) {
|
if (null !== $cacheKey) {
|
||||||
$constraintHash = spl_object_hash($constraint);
|
$constraintHash = spl_object_hash($constraint);
|
||||||
|
// instanceof Valid: In case of using a Valid constraint with many groups
|
||||||
if ($constraint instanceof Composite) {
|
// it makes a reference object get validated by each group
|
||||||
|
if ($constraint instanceof Composite || $constraint instanceof Valid) {
|
||||||
$constraintHash .= $group;
|
$constraintHash .= $group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user