Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator] Fixed default group for nested composite constraints #36365

Merged
merged 1 commit into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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