Skip to content

Commit

Permalink
bug #23722 [Form] Fixed GroupSequence with "constraints" option (Heah…
Browse files Browse the repository at this point in the history
…Dude)

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
  • Loading branch information
ogizanagi committed Sep 18, 2017
2 parents 2bf5629 + e39e7a4 commit c3518b5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
Expand Up @@ -65,18 +65,38 @@ public function validate($form, Constraint $constraint)
// Validate the data against the constraints defined
// in the form
$constraints = $config->getOption('constraints', array());
foreach ($constraints as $constraint) {
foreach ($groups as $group) {
if (in_array($group, $constraint->groups)) {
if ($validator) {
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
} else {
// 2.4 API
$this->context->validateValue($form->getData(), $constraint, 'data', $group);

if ($groups instanceof GroupSequence) {
if ($validator) {
$validator->atPath('data')->validate($form->getData(), $constraints, $groups);
} else {
// 2.4 API
foreach ($groups as $group) {
foreach ($constraints as $constraint) {
if (in_array($group, $constraint->groups)) {
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
}
}

// Prevent duplicate validation
continue 2;
if (count($this->context->getViolations()) > 0) {
break;
}
}
}
} else {
foreach ($constraints as $constraint) {
foreach ($groups as $group) {
if (in_array($group, $constraint->groups)) {
if ($validator) {
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
} else {
// 2.4 API
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
}

// Prevent duplicate validation
continue 2;
}
}
}
}
Expand Down
Expand Up @@ -342,7 +342,7 @@ function () { throw new TransformationFailedException(); }
$this->assertNoViolation();
}

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

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

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

$this->assertNoViolation();
}

public function testHandleGroupSequenceValidationGroups()
public function testHandleCallbackValidationGroups()
{
$object = $this->getMockBuilder('\stdClass')->getMock();
$options = array('validation_groups' => array($this, 'getValidationGroups'));
Expand Down
Expand Up @@ -12,14 +12,22 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;

use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest;
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validation;

class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
{
public function testSubmitValidatesData()
{
$builder = $this->factory->createBuilder(
'form',
FormTypeTest::TESTED_TYPE,
null,
array(
'validation_groups' => 'group',
Expand Down Expand Up @@ -63,8 +71,27 @@ public function testInvalidValidatorInterface()
new FormTypeValidatorExtension(null);
}

public function testGroupSequenceWithConstraintsOption()
{
$form = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory()
->create(FormTypeTest::TESTED_TYPE, null, (array('validation_groups' => new GroupSequence(array('First', 'Second')))))
->add('field', TextTypeTest::TESTED_TYPE, array(
'constraints' => array(
new Length(array('min' => 10, 'groups' => array('First'))),
new Email(array('groups' => array('Second'))),
),
))
;

$form->submit(array('field' => 'wrong'));

$this->assertCount(1, $form->getErrors(true));
}

protected function createForm(array $options = array())
{
return $this->factory->create('form', null, $options);
return $this->factory->create(FormTypeTest::TESTED_TYPE, null, $options);
}
}

0 comments on commit c3518b5

Please sign in to comment.