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 c9ac5c3
Show file tree
Hide file tree
Showing 4 changed files with 70 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 @@ -14,6 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Valid;
Expand Down Expand Up @@ -100,4 +102,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 @@ -15,11 +15,12 @@
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Valid;

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

protected function getCompositeOption()
{
Expand All @@ -37,6 +38,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 c9ac5c3

Please sign in to comment.