Skip to content

Commit

Permalink
bug #30595 Do not validate child constraints if form has no validatio…
Browse files Browse the repository at this point in the history
…n groups (maryo)

This PR was merged into the 3.4 branch.

Discussion
----------

Do not validate child constraints if form has no validation groups

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

If a form has `Valid` constraint and `validation_groups` set to an empty array (to disable validation) then its children were still validated using default validation group because `FormValidator` validated the form data using the empty array validation group here
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php#L76

and then `RecursiveContextualValidator` treats the empty array as default validation group here.

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php#L86

Commits
-------

f45f0d0 [Form] Preventing validation of children if parent with Valid constraint has no validation groups
  • Loading branch information
fabpot committed Mar 20, 2019
2 parents 059ba38 + f45f0d0 commit 1246c79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Expand Up @@ -44,6 +44,11 @@ public function validate($form, Constraint $formConstraint)
if ($form->isSubmitted() && $form->isSynchronized()) {
// Validate the form data only if transformation succeeded
$groups = self::getValidationGroups($form);

if (!$groups) {
return;
}

$data = $form->getData();

// Validate the data against its own constraints
Expand Down
Expand Up @@ -220,6 +220,28 @@ public function testDontValidateConstraintsIfNoValidationGroups()
$this->assertNoViolation();
}

public function testDontValidateChildConstraintsIfCallableNoValidationGroups()
{
$formOptions = [
'constraints' => [new Valid()],
'validation_groups' => [],
];
$form = $this->getBuilder('name', null, $formOptions)
->setCompound(true)
->setDataMapper(new PropertyPathMapper())
->getForm();
$childOptions = ['constraints' => [new NotBlank()]];
$child = $this->getCompoundForm(new \stdClass(), $childOptions);
$form->add($child);
$form->submit([]);

$this->expectNoValidate();

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

$this->assertNoViolation();
}

public function testDontValidateIfNotSynchronized()
{
$object = new \stdClass();
Expand Down

0 comments on commit 1246c79

Please sign in to comment.