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
-------
e39e7a4fa9
[Form] Fixed GroupSequence with "constraints" option
This commit is contained in:
commit
c3518b59ef
|
@ -65,18 +65,38 @@ class FormValidator extends ConstraintValidator
|
||||||
// Validate the data against the constraints defined
|
// Validate the data against the constraints defined
|
||||||
// in the form
|
// in the form
|
||||||
$constraints = $config->getOption('constraints', array());
|
$constraints = $config->getOption('constraints', array());
|
||||||
foreach ($constraints as $constraint) {
|
|
||||||
foreach ($groups as $group) {
|
if ($groups instanceof GroupSequence) {
|
||||||
if (in_array($group, $constraint->groups)) {
|
if ($validator) {
|
||||||
if ($validator) {
|
$validator->atPath('data')->validate($form->getData(), $constraints, $groups);
|
||||||
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
|
} else {
|
||||||
} else {
|
// 2.4 API
|
||||||
// 2.4 API
|
foreach ($groups as $group) {
|
||||||
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
|
foreach ($constraints as $constraint) {
|
||||||
|
if (in_array($group, $constraint->groups)) {
|
||||||
|
$this->context->validateValue($form->getData(), $constraint, 'data', $group);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent duplicate validation
|
if (count($this->context->getViolations()) > 0) {
|
||||||
continue 2;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -342,7 +342,7 @@ class FormValidatorTest extends AbstractConstraintValidatorTest
|
||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandleCallbackValidationGroups()
|
public function testHandleGroupSequenceValidationGroups()
|
||||||
{
|
{
|
||||||
$object = $this->getMockBuilder('\stdClass')->getMock();
|
$object = $this->getMockBuilder('\stdClass')->getMock();
|
||||||
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
|
$options = array('validation_groups' => new GroupSequence(array('group1', 'group2')));
|
||||||
|
@ -351,13 +351,14 @@ class FormValidatorTest extends AbstractConstraintValidatorTest
|
||||||
->getForm();
|
->getForm();
|
||||||
|
|
||||||
$this->expectValidateAt(0, 'data', $object, new GroupSequence(array('group1', 'group2')));
|
$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->validator->validate($form, new Form());
|
||||||
|
|
||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandleGroupSequenceValidationGroups()
|
public function testHandleCallbackValidationGroups()
|
||||||
{
|
{
|
||||||
$object = $this->getMockBuilder('\stdClass')->getMock();
|
$object = $this->getMockBuilder('\stdClass')->getMock();
|
||||||
$options = array('validation_groups' => array($this, 'getValidationGroups'));
|
$options = array('validation_groups' => array($this, 'getValidationGroups'));
|
||||||
|
|
|
@ -12,14 +12,22 @@
|
||||||
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
|
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
|
||||||
|
|
||||||
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
|
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\ConstraintViolationList;
|
||||||
|
use Symfony\Component\Validator\Validation;
|
||||||
|
|
||||||
class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
|
class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
|
||||||
{
|
{
|
||||||
public function testSubmitValidatesData()
|
public function testSubmitValidatesData()
|
||||||
{
|
{
|
||||||
$builder = $this->factory->createBuilder(
|
$builder = $this->factory->createBuilder(
|
||||||
'form',
|
FormTypeTest::TESTED_TYPE,
|
||||||
null,
|
null,
|
||||||
array(
|
array(
|
||||||
'validation_groups' => 'group',
|
'validation_groups' => 'group',
|
||||||
|
@ -63,8 +71,27 @@ class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
|
||||||
new FormTypeValidatorExtension(null);
|
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())
|
protected function createForm(array $options = array())
|
||||||
{
|
{
|
||||||
return $this->factory->create('form', null, $options);
|
return $this->factory->create(FormTypeTest::TESTED_TYPE, null, $options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue