Skip to content

[Form] Fixed GroupSequence with "constraints" option #23722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}