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 6a27337

Browse filesBrowse files
committed
bug #36365 [Validator] Fixed default group for nested composite constraints (HeahDude)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] Fixed default group for nested composite constraints | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #33986 | License | MIT | Doc PR | ~ Take a breath: when composite constraints are nested in a parent composite constraint without having non composite nested constraints (i.e empty), then the default group is not added, making the validator failing to validate in any group (including default), because there is no group at all, which should never happen. Commits ------- 117ee34 [Validator] Fixed default group for nested composite constraints
2 parents cd4a4bd + 117ee34 commit 6a27337
Copy full SHA for 6a27337

File tree

Expand file treeCollapse file tree

4 files changed

+62
-2
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+62
-2
lines changed

‎src/Symfony/Component/Validator/Constraints/Composite.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Constraints/Composite.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public function __construct($options = null)
8888
}
8989
}
9090

91-
$this->groups = array_keys($mergedGroups);
91+
// prevent empty composite constraint to have empty groups
92+
$this->groups = array_keys($mergedGroups) ?: [self::DEFAULT_GROUP];
9293
$this->$compositeOption = $nestedConstraints;
9394

9495
return;

‎src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,16 @@ public function testAcceptRequiredConstraintAsOneElementArray()
100100

101101
$this->assertEquals($collection1, $collection2);
102102
}
103+
104+
public function testConstraintHasDefaultGroupWithOptionalValues()
105+
{
106+
$constraint = new Collection([
107+
'foo' => new Required(),
108+
'bar' => new Optional(),
109+
]);
110+
111+
$this->assertEquals(['Default'], $constraint->groups);
112+
$this->assertEquals(['Default'], $constraint->fields['foo']->groups);
113+
$this->assertEquals(['Default'], $constraint->fields['bar']->groups);
114+
}
103115
}

‎src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/CollectionValidatorTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ public function testExtraFieldsDisallowed()
143143
->assertRaised();
144144
}
145145

146+
public function testExtraFieldsDisallowedWithOptionalValues()
147+
{
148+
$constraint = new Optional();
149+
150+
$data = $this->prepareTestData([
151+
'baz' => 6,
152+
]);
153+
154+
$this->validator->validate($data, new Collection([
155+
'fields' => [
156+
'foo' => $constraint,
157+
],
158+
'extraFieldsMessage' => 'myMessage',
159+
]));
160+
161+
$this->buildViolation('myMessage')
162+
->setParameter('{{ field }}', '"baz"')
163+
->atPath('property.path[baz]')
164+
->setInvalidValue(6)
165+
->setCode(Collection::NO_SUCH_FIELD_ERROR)
166+
->assertRaised();
167+
}
168+
146169
// bug fix
147170
public function testNullNotConsideredExtraField()
148171
{

‎src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Tests/Constraints/CompositeTest.php
+25-1Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class ConcreteComposite extends Composite
2121
{
22-
public $constraints;
22+
public $constraints = [];
2323

2424
protected function getCompositeOption()
2525
{
@@ -37,6 +37,30 @@ public function getDefaultOption()
3737
*/
3838
class CompositeTest extends TestCase
3939
{
40+
public function testConstraintHasDefaultGroup()
41+
{
42+
$constraint = new ConcreteComposite([
43+
new NotNull(),
44+
new NotBlank(),
45+
]);
46+
47+
$this->assertEquals(['Default'], $constraint->groups);
48+
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
49+
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
50+
}
51+
52+
public function testNestedCompositeConstraintHasDefaultGroup()
53+
{
54+
$constraint = new ConcreteComposite([
55+
new ConcreteComposite(),
56+
new ConcreteComposite(),
57+
]);
58+
59+
$this->assertEquals(['Default'], $constraint->groups);
60+
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
61+
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
62+
}
63+
4064
public function testMergeNestedGroupsIfNoExplicitParentGroup()
4165
{
4266
$constraint = new ConcreteComposite([

0 commit comments

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