Skip to content

Commit 4ef7f8c

Browse files
committed
Merge branch '3.1' into 3.2
* 3.1: 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 8f528dd + 8d24202 commit 4ef7f8c

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

Extension/Validator/Constraints/FormValidator.php

Lines changed: 7 additions & 2 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\Exception\UnexpectedTypeException;
@@ -47,8 +48,8 @@ public function validate($form, Constraint $constraint)
4748

4849
// Validate the data against its own constraints
4950
if ($form->isRoot() && (is_object($data) || is_array($data))) {
50-
foreach ($groups as $group) {
51-
$validator->atPath('data')->validate($form->getData(), null, $group);
51+
if (is_array($groups) && count($groups) > 0 || $groups instanceof GroupSequence && count($groups->groups) > 0) {
52+
$validator->atPath('data')->validate($form->getData(), null, $groups);
5253
}
5354
}
5455

@@ -166,6 +167,10 @@ private static function resolveValidationGroups($groups, FormInterface $form)
166167
$groups = call_user_func($groups, $form);
167168
}
168169

170+
if ($groups instanceof GroupSequence) {
171+
return $groups;
172+
}
173+
169174
return (array) $groups;
170175
}
171176
}

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: 25 additions & 13 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;
@@ -67,8 +68,7 @@ public function testValidate()
6768
->setData($object)
6869
->getForm();
6970

70-
$this->expectValidateAt(0, 'data', $object, 'group1');
71-
$this->expectValidateAt(1, 'data', $object, 'group2');
71+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
7272

7373
$this->validator->validate($form, new Form());
7474

@@ -90,12 +90,11 @@ public function testValidateConstraints()
9090
->getForm();
9191

9292
// First default constraints
93-
$this->expectValidateAt(0, 'data', $object, 'group1');
94-
$this->expectValidateAt(1, 'data', $object, 'group2');
93+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
9594

9695
// Then custom constraints
97-
$this->expectValidateValueAt(2, 'data', $object, $constraint1, 'group1');
98-
$this->expectValidateValueAt(3, 'data', $object, $constraint2, 'group2');
96+
$this->expectValidateValueAt(1, 'data', $object, $constraint1, 'group1');
97+
$this->expectValidateValueAt(2, 'data', $object, $constraint2, 'group2');
9998

10099
$this->validator->validate($form, new Form());
101100

@@ -153,7 +152,7 @@ public function testMissingConstraintIndex()
153152
$form = new FormBuilder('name', '\stdClass', $this->dispatcher, $this->factory);
154153
$form = $form->setData($object)->getForm();
155154

156-
$this->expectValidateAt(0, 'data', $object, 'Default');
155+
$this->expectValidateAt(0, 'data', $object, array('Default'));
157156

158157
$this->validator->validate($form, new Form());
159158

@@ -364,6 +363,21 @@ function () { throw new TransformationFailedException(); }
364363
$this->assertNoViolation();
365364
}
366365

366+
public function testHandleGroupSequenceValidationGroups()
367+
{
368+
$object = $this->getMockBuilder('\stdClass')->getMock();
369+
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
370+
$form = $this->getBuilder('name', '\stdClass', $options)
371+
->setData($object)
372+
->getForm();
373+
374+
$this->expectValidateAt(0, 'data', $object, new GroupSequence(array('group1', 'group2')));
375+
376+
$this->validator->validate($form, new Form());
377+
378+
$this->assertNoViolation();
379+
}
380+
367381
public function testHandleCallbackValidationGroups()
368382
{
369383
$object = $this->getMockBuilder('\stdClass')->getMock();
@@ -372,8 +386,7 @@ public function testHandleCallbackValidationGroups()
372386
->setData($object)
373387
->getForm();
374388

375-
$this->expectValidateAt(0, 'data', $object, 'group1');
376-
$this->expectValidateAt(1, 'data', $object, 'group2');
389+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
377390

378391
$this->validator->validate($form, new Form());
379392

@@ -388,7 +401,7 @@ public function testDontExecuteFunctionNames()
388401
->setData($object)
389402
->getForm();
390403

391-
$this->expectValidateAt(0, 'data', $object, 'header');
404+
$this->expectValidateAt(0, 'data', $object, array('header'));
392405

393406
$this->validator->validate($form, new Form());
394407

@@ -405,8 +418,7 @@ public function testHandleClosureValidationGroups()
405418
->setData($object)
406419
->getForm();
407420

408-
$this->expectValidateAt(0, 'data', $object, 'group1');
409-
$this->expectValidateAt(1, 'data', $object, 'group2');
421+
$this->expectValidateAt(0, 'data', $object, array('group1', 'group2'));
410422

411423
$this->validator->validate($form, new Form());
412424

@@ -544,7 +556,7 @@ public function testAppendPropertyPath()
544556
->setData($object)
545557
->getForm();
546558

547-
$this->expectValidateAt(0, 'data', $object, 'Default');
559+
$this->expectValidateAt(0, 'data', $object, array('Default'));
548560

549561
$this->validator->validate($form, new Form());
550562

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)