8000 bug #23722 [Form] Fixed GroupSequence with "constraints" option (Heah… · symfony/symfony@c3518b5 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c3518b5

Browse files
committed
bug #23722 [Form] Fixed GroupSequence with "constraints" option (HeahDude)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Fixed GroupSequence with "constraints" option | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22373 | License | MIT | Doc PR | ~ Commits ------- e39e7a4 [Form] Fixed GroupSequence with "constraints" option
2 parents 2bf5629 + e39e7a4 commit c3518b5

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,38 @@ public function validate($form, Constraint $constraint)
6565
// Validate the data against the constraints defined
6666
// in the form
6767
$constraints = $config->getOption('constraints', array());
68-
foreach ($constraints as $constraint) {
69-
foreach ($groups as $group) {
70-
if (in_array($group, $constraint->groups)) {
71-
if ($validator) {
72-
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
73-
} else {
74-
// 2.4 API
75-
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
68+
69+
if ($groups instanceof GroupSequence) {
70+
if ($validator) {
71+
$validator->atPath('data')->validate($form->getData(), $constraints, $groups);
72+
} else {
73+
// 2.4 API
74+
foreach ($groups as $group) {
75+
foreach ($constraints as $constraint) {
76+
if (in_array($group, $constraint->groups)) {
77+
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
78+
}
7679
}
7780

78-
// Prevent duplicate validation
79-
continue 2;
81+
if (count($this->context->getViolations()) > 0) {
82+
break;
83+
}
84+
}
85+
}
86+
} else {
87+
foreach ($constraints as $constraint) {
88+
foreach ($groups as $group) {
89+
if (in_array($group, $constraint->groups)) {
90+
if ($validator) {
91+
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
92+
} else {
93+
// 2.4 API
94+
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
95+
}
96+
97+
// Prevent duplicate validation
98+
continue 2;
99+
}
80100
}
81101
}
82102
}

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function () { throw new TransformationFailedException(); }
342342
$this->assertNoViolation();
343343
}
344344

345-
public function testHandleCallbackValidationGroups()
345+
public function testHandleGroupSequenceValidationGroups()
346346
{
347347
$object = $this->getMockBuilder('\stdClass')->getMock();
348348
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
@@ -351,13 +351,14 @@ public function testHandleCallbackValidationGroups()
351351
->getForm();
352352

353353
$this->expectValidateAt(0, 'data', $object, new GroupSequence(array('group1', 'group2')));
354+
$this->expectValidateAt(1, 'data', $object, new GroupSequence(array('group1', 'group2')));
354355

355356
$this->validator->validate($form, new Form());
356357

357358
$this->assertNoViolation();
358359
}
359360

360-
public function testHandleGroupSequenceValidationGroups()
361+
public function testHandleCallbackValidationGroups()
361362
{
362363
$object = $this->getMockBuilder('\stdClass')->getMock();
363364
$options = array('validation_groups' => array($this, 'getValidationGroups'));

src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

1414
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
15+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
16+
use Symfony\Component\Form\Forms;
17+
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
18+
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
19+
use Symfony\Component\Validator\Constraints\Email;
20+
use Symfony\Component\Validator\Constraints\GroupSequence;
21+
use Symfony\Component\Validator\Constraints\Length;
1522
use Symfony\Component\Validator\ConstraintViolationList;
23+
use Symfony\Component\Validator\Validation;
1624

1725
class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
1826
{
1927
public function testSubmitValidatesData()
2028
{
2129
$builder = $this->factory->createBuilder(
22-
'form',
30+
FormTypeTest::TESTED_TYPE,
2331
null,
2432
array(
2533
'validation_groups' => 'group',
@@ -63,8 +71,27 @@ public function testInvalidValidatorInterface()
6371
new FormTypeValidatorExtension(null);
6472
}
6573

74+
public function testGroupSequenceWithConstraintsOption()
75+
{
76+
$form = Forms::createFormFactoryBuilder()
77+
->addExtension(new ValidatorExtension(Validation::createValidator()))
78+
->getFormFactory()
79+
->create(FormTypeTest::TESTED_TYPE, null, (array('validation_groups' => new GroupSequence(array('First', 'Second')))))
80+
->add('field', TextTypeTest::TESTED_TYPE, array(
81+
'constraints' => array(
82+
new Length(array('min' => 10, 'groups' => array('First'))),
83+
new Email(array('groups' => array('Second'))),
84+
),
85+
))
86+
;
87+
88+
$form->submit(array('field' => 'wrong'));
89+
90+
$this->assertCount(1, $form->getErrors(true));
91+
}
92+
6693
protected function createForm(array $options = array())
6794
{
68-
return $this->factory->create('form', null, $options);
95+
return $this->factory->create(FormTypeTest::TESTED_TYPE, null, $options);
6996
}
7097
}

0 commit comments

Comments
 (0)
0