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 fc8dc91

Browse filesBrowse files
committed
bug #29499 [Validator] Fixed grouped composite constraints (HeahDude)
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
2 parents 6f5356c + b53d911 commit fc8dc91
Copy full SHA for fc8dc91

File tree

2 files changed

+43
-0
lines changed
Filter options

2 files changed

+43
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Symfony\Component\Validator\Tests\Validator;
1313

1414
use Symfony\Component\Translation\IdentityTranslator;
15+
use Symfony\Component\Validator\Constraints\All;
16+
use Symfony\Component\Validator\Constraints\Collection;
17+
use Symfony\Component\Validator\Constraints\Length;
18+
use Symfony\Component\Validator\Constraints\NotBlank;
1519
use Symfony\Component\Validator\ConstraintValidatorFactory;
1620
use Symfony\Component\Validator\Context\ExecutionContextFactory;
1721
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
@@ -95,4 +99,38 @@ public function testRelationBetweenChildAAndChildB()
9599

96100
$validator->validate($entity, null, array());
97101
}
102+
103+
public function testCollectionConstraintValidateAllGroupsForNestedConstraints()
104+
{
105+
$this->metadata->addPropertyConstraint('data', new Collection(array('fields' => array(
106+
'one' => array(new NotBlank(array('groups' => 'one')), new Length(array('min' => 2, 'groups' => 'two'))),
107+
'two' => array(new NotBlank(array('groups' => 'two'))),
108+
))));
109+
110+
$entity = new Entity();
111+
$entity->data = array('one' => 't', 'two' => '');
112+
113+
$violations = $this->validator->validate($entity, null, array('one', 'two'));
114+
115+
$this->assertCount(2, $violations);
116+
$this->assertInstanceOf(Length::class, $violations->get(0)->getConstraint());
117+
$this->assertInstanceOf(NotBlank::class, $violations->get(1)->getConstraint());
118+
}
119+
120+
public function testAllConstraintValidateAllGroupsForNestedConstraints()
121+
{
122+
$this->metadata->addPropertyConstraint('data', new All(array('constraints' => array(
123+
new NotBlank(array('groups' => 'one')),
124+
new Length(array('min' => 2, 'groups' => 'two')),
125+
))));
126+
127+
$entity = new Entity();
128+
$entity->data = array('one' => 't', 'two' => '');
129+
130+
$violations = $this->validator->validate($entity, null, array('one', 'two'));
131+
132+
$this->assertCount(2, $violations);
133+
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
134+
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
135+
}
98136
}

‎src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Validator;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\Composite;
1516
use Symfony\Component\Validator\Constraints\GroupSequence;
1617
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1718
use Symfony\Component\Validator\Context\ExecutionContext;
@@ -787,6 +788,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
787788
if (null !== $cacheKey) {
788789
$constraintHash = spl_object_hash($constraint);
789790

791+
if ($constraint instanceof Composite) {
792+
$constraintHash .= $group;
793+
}
794+
790795
if ($context->isConstraintValidated($cacheKey, $constraintHash)) {
791796
continue;
792797
}

0 commit comments

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