Skip to content

Commit

Permalink
[Validator] Fixed default group for nested composite constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Apr 6, 2020
1 parent 5da141b commit 4a79a21
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Composite.php
Expand Up @@ -83,6 +83,13 @@ public function __construct($options = null)
$mergedGroups = [];

foreach ($nestedConstraints as $constraint) {
if ($constraint instanceof self && !$constraint->groups) {
// this nested composite constraint is initialized without groups
// when it happens, this one and its nested constraints needs to be
// implicitly added in the default group
$constraint->groups = [self::DEFAULT_GROUP];
}

foreach ($constraint->groups as $group) {
$mergedGroups[$group] = true;
}
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 4a79a21

Please sign in to comment.