Skip to content

Commit 1246c79

Browse files
committed
bug symfony#30595 Do not validate child constraints if form has no validation 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
2 parents 059ba38 + f45f0d0 commit 1246c79

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public function validate($form, Constraint $formConstraint)
4444
if ($form->isSubmitted() && $form->isSynchronized()) {
4545
// Validate the form data only if transformation succeeded
4646
$groups = self::getValidationGroups($form);
47+
48+
if (!$groups) {
49+
return;
50+
}
51+
4752
$data = $form->getData();
4853

4954
// Validate the data against its own constraints

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ public function testDontValidateConstraintsIfNoValidationGroups()
220220
$this->assertNoViolation();
221221
}
222222

223+
public function testDontValidateChildConstraintsIfCallableNoValidationGroups()
224+
{
225+
$formOptions = [
226+
'constraints' => [new Valid()],
227+
'validation_groups' => [],
228+
];
229+
$form = $this->getBuilder('name', null, $formOptions)
230+
->setCompound(true)
231+
->setDataMapper(new PropertyPathMapper())
232+
->getForm();
233+
$childOptions = ['constraints' => [new NotBlank()]];
234+
$child = $this->getCompoundForm(new \stdClass(), $childOptions);
235+
$form->add($child);
236+
$form->submit([]);
237+
238+
$this->expectNoValidate();
239+
240+
$this->validator->validate($form, new Form());
241+
242+
$this->assertNoViolation();
243+
}
244+
223245
public function testDontValidateIfNotSynchronized()
224246
{
225247
$object = new \stdClass();

0 commit comments

Comments
 (0)