-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Propagate embedded groups for collection validator #17696
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
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 |
---|---|---|
|
@@ -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, $fieldConstraint->groups); | ||
} else { | ||
// 2.4 API | ||
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. It's not the scope of this PR but shouldn't this be removed in 2.7 ? 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. Why removed ? I check on the 2.8 and 3.0 branch and the code is the same. |
||
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']'); | ||
$context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $context->getGroup()); | ||
} | ||
} | ||
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,7 +271,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)) { | ||
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 looks weird to me. Why is this necessary? 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. It's necessary because for embedded constraint with multiple validation groups, the validator is execute for each validation groups. With this example:
We have two violations but we want only one. Is there another way to fix that ? |
||
$filteredConstraints[] = $constraint; | ||
} | ||
} | ||
|
||
foreach ($filteredConstraints as $constraint) { | ||
$validator = $this->globalContext->getValidatorFactory()->getInstance($constraint); | ||
$validator->initialize($this); | ||
$validator->validate($value, $constraint); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,11 @@ | |
|
||
namespace Symfony\Component\Validator\Tests\Validator; | ||
|
||
use Symfony\Component\Validator\Constraints\All; | ||
use Symfony\Component\Validator\Constraints\Callback; | ||
use Symfony\Component\Validator\Constraints\Collection; | ||
use Symfony\Component\Validator\Constraints\GroupSequence; | ||
use Symfony\Component\Validator\Constraints\Range; | ||
use Symfony\Component\Validator\Constraints\Valid; | ||
use Symfony\Component\Validator\ConstraintViolationInterface; | ||
use Symfony\Component\Validator\ExecutionContextInterface; | ||
|
@@ -1079,6 +1082,50 @@ public function testValidateMultipleGroups() | |
$this->assertCount(2, $violations); | ||
} | ||
|
||
public function testValidateMultipleGroupsForCollectionConstraint() | ||
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 belongs in CollectionValidatorTest. Also, I'm missing tests for what is happening when one or more groups are set on the
The same applies to AllValidator. 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. @blazarecki Can you address this comment? |
||
{ | ||
$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')), | ||
), | ||
), | ||
) | ||
)); | ||
|
||
$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')), | ||
), | ||
) | ||
)); | ||
|
||
$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 testReplaceDefaultGroupByGroupSequenceObject() | ||
{ | ||
$entity = new Entity(); | ||
|
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.
Shouldn't this be
$context->getGroup()
too?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.
Hi, @xabbuh
I try to use the context but the test fail, it's the only way I find to make it work:
Do you have another idea ?