Skip to content

Commit

Permalink
bug #36365 [Validator] Fixed default group for nested composite const…
Browse files Browse the repository at this point in the history
…raints (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
  • Loading branch information
fabpot committed Apr 12, 2020
2 parents cd4a4bd + 117ee34 commit 6a27337
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Composite.php
Expand Up @@ -88,7 +88,8 @@ public function __construct($options = null)
}
}

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

return;
Expand Down
Expand Up @@ -100,4 +100,16 @@ public function testAcceptRequiredConstraintAsOneElementArray()

$this->assertEquals($collection1, $collection2);
}

public function testConstraintHasDefaultGroupWithOptionalValues()
{
$constraint = new Collection([
'foo' => new Required(),
'bar' => new Optional(),
]);

$this->assertEquals(['Default'], $constraint->groups);
$this->assertEquals(['Default'], $constraint->fields['foo']->groups);
$this->assertEquals(['Default'], $constraint->fields['bar']->groups);
}
}
Expand Up @@ -143,6 +143,29 @@ public function testExtraFieldsDisallowed()
->assertRaised();
}

public function testExtraFieldsDisallowedWithOptionalValues()
{
$constraint = new Optional();

$data = $this->prepareTestData([
'baz' => 6,
]);

$this->validator->validate($data, new Collection([
'fields' => [
'foo' => $constraint,
],
'extraFieldsMessage' => 'myMessage',
]));

$this->buildViolation('myMessage')
->setParameter('{{ field }}', '"baz"')
->atPath('property.path[baz]')
->setInvalidValue(6)
->setCode(Collection::NO_SUCH_FIELD_ERROR)
->assertRaised();
}

// bug fix
public function testNullNotConsideredExtraField()
{
Expand Down
Expand Up @@ -19,7 +19,7 @@

class ConcreteComposite extends Composite
{
public $constraints;
public $constraints = [];

protected function getCompositeOption()
{
Expand All @@ -37,6 +37,30 @@ public function getDefaultOption()
*/
class CompositeTest extends TestCase
{
public function testConstraintHasDefaultGroup()
{
$constraint = new ConcreteComposite([
new NotNull(),
new NotBlank(),
]);

$this->assertEquals(['Default'], $constraint->groups);
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
}

public function testNestedCompositeConstraintHasDefaultGroup()
{
$constraint = new ConcreteComposite([
new ConcreteComposite(),
new ConcreteComposite(),
]);

$this->assertEquals(['Default'], $constraint->groups);
$this->assertEquals(['Default'], $constraint->constraints[0]->groups);
$this->assertEquals(['Default'], $constraint->constraints[1]->groups);
}

public function testMergeNestedGroupsIfNoExplicitParentGroup()
{
$constraint = new ConcreteComposite([
Expand Down

0 comments on commit 6a27337

Please sign in to comment.