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] Propagate embedded groups for collection validator #25888

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Expand Up @@ -44,12 +44,12 @@ public function validate($value, Constraint $constraint)
$validator = $context->getValidator()->inContext($context);

foreach ($value as $key => $element) {
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints);
$validator->atPath('['.$key.']')->validate($element, $constraint->constraints, array($context->getGroup()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only subsets are allowed on traversable node. This would be a broken feature not a bug fix.

}
} else {
// 2.4 API
foreach ($value as $key => $element) {
$context->validateValue($element, $constraint->constraints, '['.$key.']');
$context->validateValue($element, $constraint->constraints, '['.$key.']', $context->getGroup());
}
}
}
Expand Down
Expand Up @@ -60,10 +60,10 @@ public function validate($value, Constraint $constraint)
$context->getValidator()
->inContext($context)
->atPath('['.$field.']')
->validate($value[$field], $fieldConstraint->constraints);
->validate($value[$field], $fieldConstraint->constraints, array($context->getGroup()));
} else {
// 2.4 API
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']');
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $context->getGroup());
}
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/ExecutionContext.php
Expand Up @@ -244,7 +244,15 @@ public function getMetadataFactory()
*/
private function executeConstraintValidators($value, array $constraints)
{
// Filter constraints with the context group.
$filteredConstraints = array();
foreach ($constraints as $constraint) {
if (in_array($this->getGroup(), $constraint->groups)) {
$filteredConstraints[] = $constraint;
}
}

foreach ($filteredConstraints as $constraint) {
$validator = $this->globalContext->getValidatorFactory()->getInstance($constraint);
$validator->initialize($this);
$validator->validate($value, $constraint);
Expand Down
Expand Up @@ -226,7 +226,7 @@ protected function expectValidateAt($i, $propertyPath, $value, $group)
->with($value, $this->logicalOr(null, array()), $group);
}

protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = null)
protected function expectValidateValueAt($i, $propertyPath, $value, $constraints, $group = array('MyGroup'))
{
$contextualValidator = $this->context->getValidator()->inContext($this->context);
$contextualValidator->expects($this->at(2 * $i))
Expand Down
Expand Up @@ -386,4 +386,64 @@ public function testObjectShouldBeLeftUnchanged()
'foo' => 3,
), (array) $value);
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testGroupShouldBePassedToTheContainingConstraint()
{
$value = array('baz' => 2);

$constraint = new Collection(
array(
'fields' => array(
'baz' => array(
new Range(array('min' => 1, 'groups' => 'bar')),
),
),
'groups' => 'foo',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is wrong and breaks BC. Groups should be array('foo, 'bar') here, and the test would be green already, without the changes introduced in this PR.

)
);

$data = $this->prepareTestData($value);
$this->validator->validate($data, $constraint);
}

/**
* @dataProvider multipleGroupsForCollectionProvider
*/
public function testValidateMultipleGroupsForCollectionConstraint($fooGroups, $barGroups, $collectionGroups)
{
$value = array('baz' => 2);

$constraint = new Collection(
array(
'fields' => array(
'baz' => array(
$fooConstraint = new Range(array('min' => 3, 'minMessage' => 'Group foo', 'groups' => $fooGroups)),
$barConstraint = new Range(array('min' => 5, 'minMessage' => 'Group bar', 'groups' => $barGroups)),
),
),
'groups' => $collectionGroups,
)
);

$data = $this->prepareTestData($value);

$this->expectValidateValueAt(0, '[baz]', $value['baz'], array($fooConstraint, $barConstraint), array('MyGroup'));

$this->validator->validate($data, $constraint);
}

public static function multipleGroupsForCollectionProvider()
{
return array(
array(array('foo', 'bar'), array('foo', 'bar'), array('foo', 'bar')),
array(array('foo', 'bar'), array('bar'), array('foo', 'bar')),
array(array('foo'), array('foo', 'bar'), array('foo', 'bar')),
array(array('foo'), array('bar'), array('foo', 'bar')),
array(array('foo'), array('foo'), array('foo', 'bar')),
array(array('foo'), array('foo'), array('foo')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're passing foo and bar for every case of collection groups above, but not here, while there is no bar group on any entry.
Are there some cases actually failing before your changes?

);
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Validator/Tests/LegacyValidatorTest.php
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Component\Validator\Tests;

use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\MetadataFactoryInterface;
Expand Down Expand Up @@ -39,4 +42,48 @@ public function testValidateValueRejectsValid()
{
$this->validator->validateValue(new Entity(), new Valid());
}

public function testValidateMultipleGroupsForCollectionConstraint()
{
$entity = new Entity();
$entity->firstName = array('baz' => 2);

$this->metadata->addPropertyConstraint('firstName', new Collection(
array(
'fields' => array(
'baz' => array(
new Range(array('min' => 3, 'minMessage' => 'Group foo', 'groups' => 'foo')),
new Range(array('min' => 5, 'minMessage' => 'Group bar', 'groups' => 'bar')),
),
),
)
));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no group set on the collection contraint, group on entry should never be validated (since it won't ever be a subset).


$violations = $this->validate($entity, null, array('foo', 'bar'));

$this->assertCount(2, $violations);
$this->assertSame('Group foo', $violations[0]->getMessage());
$this->assertSame('Group bar', $violations[1]->getMessage());
}

public function testValidateMultipleGroupsForAllConstraint()
{
$entity = new Entity();
$entity->firstName = array(1);

$this->metadata->addPropertyConstraint('firstName', new All(
array(
'constraints' => array(
new Range(array('min' => 3, 'minMessage' => 'Group foo', 'groups' => 'foo')),
new Range(array('min' => 5, 'minMessage' => 'Group bar', 'groups' => 'bar')),
),
)
));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


$violations = $this->validate($entity, null, array('foo', 'bar'));

$this->assertCount(2, $violations);
$this->assertSame('Group foo', $violations[0]->getMessage());
$this->assertSame('Group bar', $violations[1]->getMessage());
}
}