Skip to content

Commit 96281dc

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: use HHVM 3.15 to run tests [TwigBridge] fix Twig 2.x compatibility removed some PHP CS Fixer rules Improve language [Console] SymfonyStyle: Escape trailing backslashes in user texts Mention the community review guide [Form] fix group sequence based validation [Console] Fix question formatting using SymfonyStyle::ask()
2 parents 0f7d8d8 + 753870c commit 96281dc

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

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\Constraints\Valid;
1718
use Symfony\Component\Validator\ConstraintValidator;
1819
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -50,10 +51,12 @@ public function validate($form, Constraint $constraint)
5051

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

239+
if ($groups instanceof GroupSequence) {
240+
return $groups;
241+
}
242+
236243
return (array) $groups;
237244
}
238245
}

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

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 26 additions & 15 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\Constraints\Valid;
@@ -72,8 +73,7 @@ public function testValidate()
7273
->setData($object)
7374
->getForm();
7475

75-
$this->expectValidateAt(0, 'data', $object, 'group1');
76-
$this->expectValidateAt(1, 'data', $object, 'group2');
76+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
7777

7878
$this->validator->validate($form, new Form());
7979

@@ -95,12 +95,11 @@ public function testValidateConstraints()
9595
->getForm();
9696

9797
// First default constraints
98-
$this->expectValidateAt(0, 'data', $object, 'group1');
99-
$this->expectValidateAt(1, 'data', $object, 'group2');
98+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
10099

101100
// Then custom constraints
102-
$this->expectValidateValueAt(2, 'data', $object, $constraint1, 'group1');
103-
$this->expectValidateValueAt(3, 'data', $object, $constraint2, 'group2');
101+
$this->expectValidateValueAt(1, 'data', $object, $constraint1, 'group1');
102+
$this->expectValidateValueAt(2, 'data', $object, $constraint2, 'group2');
104103

105104
$this->validator->validate($form, new Form());
106105

@@ -121,8 +120,7 @@ public function testValidateIfParentWithCascadeValidation()
121120

122121
$form->setData($object);
123122

124-
$this->expectValidateAt(0, 'data', $object, 'group1');
125-
$this->expectValidateAt(1, 'data', $object, 'group2');
123+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
126124

127125
$this->validator->validate($form, new Form());
128126

@@ -180,7 +178,7 @@ public function testMissingConstraintIndex()
180178
$form = new FormBuilder('name', '\stdClass', $this->dispatcher, $this->factory);
181179
$form = $form->setData($object)->getForm();
182180

183-
$this->expectValidateAt(0, 'data', $object, 'Default');
181+
$this->expectValidateAt(0, 'data', $object, array('Default'));
184182

185183
$this->validator->validate($form, new Form());
186184

@@ -392,15 +390,29 @@ function () { throw new TransformationFailedException(); }
392390
}
393391

394392
public function testHandleCallbackValidationGroups()
393+
{
394+
$object = $this->getMockBuilder('\stdClass')->getMock();
395+
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
396+
$form = $this->getBuilder('name', '\stdClass', $options)
397+
->setData($object)
398+
->getForm();
399+
400+
$this->expectValidateAt(0, 'data', $object, new GroupSequence(array('group1', 'group2')));
401+
402+
$this->validator->validate($form, new Form());
403+
404+
$this->assertNoViolation();
405+
}
406+
407+
public function testHandleGroupSequenceValidationGroups()
395408
{
396409
$object = $this->getMockBuilder('\stdClass')->getMock();
397410
$options = array('validation_groups' => array($this, 'getValidationGroups'));
398411
$form = $this->getBuilder('name', '\stdClass', $options)
399412
->setData($object)
400413
->getForm();
401414

402-
$this->expectValidateAt(0, 'data', $object, 'group1');
403-
$this->expectValidateAt(1, 'data', $object, 'group2');
415+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
404416

405417
$this->validator->validate($form, new Form());
406418

@@ -415,7 +427,7 @@ public function testDontExecuteFunctionNames()
415427
->setData($object)
416428
->getForm();
417429

418-
$this->expectValidateAt(0, 'data', $object, 'header');
430+
$this->expectValidateAt(0, 'data', $object, array('header'));
419431

420432
$this->validator->validate($form, new Form());
421433

@@ -432,8 +444,7 @@ public function testHandleClosureValidationGroups()
432444
->setData($object)
433445
->getForm();
434446

435-
$this->expectValidateAt(0, 'data', $object, 'group1');
436-
$this->expectValidateAt(1, 'data', $object, 'group2');
447+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
437448

438449
$this->validator->validate($form, new Form());
439450

@@ -571,7 +582,7 @@ public function testAppendPropertyPath()
571582
->setData($object)
572583
->getForm();
573584

574-
$this->expectValidateAt(0, 'data', $object, 'Default');
585+
$this->expectValidateAt(0, 'data', $object, array('Default'));
575586

576587
$this->validator->validate($form, new Form());
577588

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)