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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is wrong and breaks BC. Groups should be |
||
) | ||
); | ||
|
||
$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')), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're passing |
||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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')), | ||
), | ||
), | ||
) | ||
)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')), | ||
), | ||
) | ||
)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} |
There was a problem hiding this comment.
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.