8000 bug #20975 [Form] fix group sequence based validation (xabbuh) · symfony/symfony@2f24e69 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f24e69

Browse files
committed
bug #20975 [Form] fix group sequence based validation (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] fix group sequence based validation | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20929 | License | MIT | Doc PR | Commits ------- fb91f74 [Form] fix group sequence based validation
2 parents 3008fa3 + fb91f74 commit 2f24e69

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\FormInterface;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Constraints\GroupSequence;
1617
use Symfony\Component\Validator\ConstraintValidator;
1718
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1819
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -49,10 +50,12 @@ public function validate($form, Constraint $constraint)
4950

5051
// Validate the data against its own constraints
5152
if (self::allowDataWalking($form)) {
52-
foreach ($groups as $group) {
53-
if ($validator) {
54-
$validator->atPath('data')->validate($form->getData(), null, $group);
55-
} else {
53+
if ($validator) {
54+
if (is_array($groups) && count($groups) > 0 || $groups instanceof GroupSequence && count($groups->groups) > 0) {
55+
$validator->atPath('data')->validate($form->getData(), null, $groups);
56+
}
57+
} else {
58+
foreach ($groups as $group) {
5659
// 2.4 API
5760
$this->context->validate($form->getData(), 'data', $group, true);
5861
}
@@ -218,6 +221,10 @@ private static function resolveValidationGroups($groups, FormInterface $form)
218221
$groups = call_user_func($groups, $form);
219222
}
220223

224+
if ($groups instanceof GroupSequence) {
225+
return $groups;
226+
}
227+
221228
return (array) $groups;
222229
}
223230
}

src/Symfony/Component/Form/Extension/Validator/Type/BaseValidatorExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\AbstractTypeExtension;
1515
use Symfony\Component\OptionsResolver\Options;
1616
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
use Symfony\Component\Validator\Constraints\GroupSequence;
1718

1819
/**
1920
* Encapsulates common logic of {@link FormTypeValidatorExtension} and
@@ -42,6 +43,10 @@ public function configureOptions(OptionsResolver $resolver)
4243
return $groups;
4344
}
4445

46+
if ($groups instanceof GroupSequence) {
47+
return $groups;
48+
}
49+
4550
return (array) $groups;
4651
};
4752

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
1919
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
2020
use Symfony\Component\Form\SubmitButtonBuilder;
21+
use Symfony\Component\Validator\Constraints\GroupSequence;
2122
use Symfony\Component\Validator\Constraints\NotNull;
2223
use Symfony\Component\Validator\Constraints\NotBlank;
2324
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
@@ -70,8 +71,7 @@ public function testValidate()
7071
->setData($object)
7172
->getForm();
7273

73-
$this->expectValidateAt(0, 'data', $object, 'group1');
74-
$this->expectValidateAt(1, 'data', $object, 'group2');
74+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
7575

7676
$this->validator->validate($form, new Form());
7777

@@ -93,12 +93,11 @@ public function testValidateConstraints()
9393
->getForm();
9494

9595
// First default constraints
96-
$this->expectValidateAt(0, 'data', $object, 'group1');
97-
$this->expectValidateAt(1, 'data', $object, 'group2');
96+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
9897

9998
// Then custom constraints
100-
$this->expectValidateValueAt(2, 'data', $object, $constraint1, 'group1');
101-
$this->expectValidateValueAt(3, 'data', $object, $constraint2, 'group2');
99+
$this->expectValidateValueAt(1, 'data', $object, $constraint1, 'group1');
100+
$this->expectValidateValueAt(2, 'data', $object, $constraint2, 'group2');
102101

103102
$this->validator->validate($form, new Form());
104103

@@ -132,7 +131,7 @@ public function testMissingConstraintIndex()
132131
$form = new FormBuilder('name', '\stdClass', $this->dispatcher, $this->factory);
133132
$form = $form->setData($object)->getForm();
134133

135-
$this->expectValidateAt(0, 'data', $object, 'Default');
134+
$this->expectValidateAt(0, 'data', $object, array('Default'));
136135

137136
$this->validator->validate($form, new Form());
138137

@@ -344,15 +343,29 @@ function () { throw new TransformationFailedException(); }
344343
}
345344

346345
public function testHandleCallbackValidationGroups()
346+
{
347+
$object = $this->getMockBuilder('\stdClass')->getMock();
348+
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
349+
$form = $this->getBuilder('name', '\stdClass', $options)
350+
->setData($object)
351+
->getForm();
352+
353+
$this->expectValidateAt(0, 'data', $object, new GroupSequence(array('group1', 'group2')));
354+
355+
$this->validator->validate($form, new Form());
356+
357+
$this->assertNoViolation();
358+
}
359+
360+
public function testHandleGroupSequenceValidationGroups()
347361
{
348362
$object = $this->getMockBuilder('\stdClass')->getMock();
349363
$options = array('validation_groups' => array($this, 'getValidationGroups'));
350364
$form = $this->getBuilder('name', '\stdClass', $options)
351365
->setData($object)
352366
->getForm();
353367

354-
$this->expectValidateAt(0, 'data', $object, 'group1');
355-
$this->expectValidateAt(1, 'data', $object, 'group2');
368+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
356369

357370
$this->validator->validate($form, new Form());
358371

@@ -367,7 +380,7 @@ public function testDontExecuteFunctionNames()
367380
->setData($object)
368381
->getForm();
369382

370-
$this->expectValidateAt(0, 'data', $object, 'header');
383+
$this->expectValidateAt(0, 'data', $object, array('header'));
371384

372385
$this->validator->validate($form, new Form());
373386

@@ -384,8 +397,7 @@ public function testHandleClosureValidationGroups()
384397
->setData($object)
385398
->getForm();
386399

387-
$this->expectValidateAt(0, 'data', $object, 'group1');
388-
$this->expectValidateAt(1, 'data', $object, 'group2');
400+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
389401

390402
$this->validator->validate($form, new Form());
391403

@@ -411,7 +423,7 @@ public function testUseValidationGroupOfClickedButton()
411423

412424
$parent->submit(array('name' => $object, 'submit' => ''));
413425

414-
$this->expectValidateAt(0, 'data', $object, 'button_group');
426+
$this->expectValidateAt(0, 'data', $object, array('button_group'));
415427

416428
$this->validator->validate($form, new Form());
417429

@@ -437,7 +449,7 @@ public function testDontUseValidationGroupOfUnclickedButton()
437449

438450
$form->setData($object);
439451

440-
$this->expectValidateAt(0, 'data', $object, 'form_group');
452+
$this->expectValidateAt(0, 'data', $object, array('form_group'));
441453

442454
$this->validator->validate($form, new Form());
443455

@@ -461,7 +473,7 @@ public function testUseInheritedValidationGroup()
461473

462474
$form->setData($object);
463475

464-
$this->expectValidateAt(0, 'data', $object, 'group');
476+
$this->expectValidateAt(0, 'data', $object, array('group'));
465477

466478
$this->validator->validate($form, new Form());
467479

@@ -485,8 +497,7 @@ public function testUseInheritedCallbackValidationGroup()
485497

486498
$form->setData($object);
487499

488-
$this->expectValidateAt(0, 'data', $object, 'group1');
489-
$this->expectValidateAt(1, 'data', $object, 'group2');
500+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
490501

491502
$this->validator->validate($form, new Form());
492503

@@ -512,8 +523,7 @@ public function testUseInheritedClosureValidationGroup()
512523

513524
$form->setData($object);
514525

515-
$this->expectValidateAt(0, 'data', $object, 'group1');
516-
$this->expectValidateAt(1, 'data', $object, 'group2');
526+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
517527

518528
$this->validator->validate($form, new Form());
519529

@@ -527,7 +537,7 @@ public function testAppendPropertyPath()
527537
->setData($object)
528538
->getForm();
529539

530-
$this->expectValidateAt(0, 'data', $object, 'Default');
540+
$this->expectValidateAt(0, 'data', $object, array('Default'));
531541

532542
$this->validator->validate($form, new Form());
533543

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

1414
use Symfony\Component\Form\Test\FormInterface;
15+
use Symfony\Component\Validator\Constraints\GroupSequence;
1516

1617
/**
1718
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -70,5 +71,14 @@ public function testValidationGroupsCanBeSetToClosure()
7071
$this->assertInternalType('callable', $form->getConfig()->getOption('validation_groups'));
7172
}
7273

74+
public function testValidationGroupsCanBeSetToGroupSequence()
75+
{
76+
$form = $this->createForm(array(
77+
'validation_groups' => new GroupSequence(array('group1', 'group2')),
78+
));
79+
80+
$this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $form->getConfig()->getOption('validation_groups'));
81+
}
82+
7383
abstract protected function createForm(array $options = array());
7484
}

0 commit comments

Comments
 (0)
0