Skip to content

Commit

Permalink
bug #29499 [Validator] Fixed grouped composite constraints (HeahDude)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] Fixed grouped composite constraints

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17675, #25888, #23480
| License       | MIT
| Doc PR        | ~

From Lisbon :). Thanks @stof, @xabbuh for your help to finally fix this old issue.

Commits
-------

b53d911 [Validator] Fixed grouped composite constraints
  • Loading branch information
stof committed Dec 8, 2018
2 parents 6f5356c + b53d911 commit fc8dc91
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Expand Up @@ -12,6 +12,10 @@
namespace Symfony\Component\Validator\Tests\Validator;

use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
Expand Down Expand Up @@ -95,4 +99,38 @@ public function testRelationBetweenChildAAndChildB()

$validator->validate($entity, null, array());
}

public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
{
$this->metadata->addPropertyConstraint('data', new Collection(array('fields' => array(
'one' => array(new NotBlank(array('groups' => 'one')), new Length(array('min' => 2, 'groups' => 'two'))),
'two' => array(new NotBlank(array('groups' => 'two'))),
))));

$entity = new Entity();
$entity->data = array('one' => 't', 'two' => '');

$violations = $this->validator->validate($entity, null, array('one', 'two'));

$this->assertCount(2, $violations);
$this->assertInstanceOf(Length::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
}

public function testAllConstraintValidateAllGroupsForNestedConstraints()
{
$this->metadata->addPropertyConstraint('data', new All(array('constraints' => array(
new NotBlank(array('groups' => 'one')),
new Length(array('min' => 2, 'groups' => 'two')),
))));

$entity = new Entity();
$entity->data = array('one' => 't', 'two' => '');

$violations = $this->validator->validate($entity, null, array('one', 'two'));

$this->assertCount(2, $violations);
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Validator;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
Expand Down Expand Up @@ -787,6 +788,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
if (null !== $cacheKey) {
$constraintHash = spl_object_hash($constraint);

if ($constraint instanceof Composite) {
$constraintHash .= $group;
}

if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
continue;
}
Expand Down

0 comments on commit fc8dc91

Please sign in to comment.