Skip to content

Commit 1f83212

Browse files
committed
bug symfony#37103 [Form] switch the context when validating nested forms (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] switch the context when validating nested forms | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#37072 | License | MIT | Doc PR | Commits ------- 38135de switch the context when validating nested forms
2 parents 816b6ea + 38135de commit 1f83212

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function validate($form, Constraint $formConstraint)
9090
// in different steps without breaking early enough
9191
$this->resolvedGroups[$field] = (array) $group;
9292
$fieldFormConstraint = new Form();
93+
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
9394
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
9495
}
9596
}
@@ -129,6 +130,7 @@ public function validate($form, Constraint $formConstraint)
129130
if ($field->isSubmitted()) {
130131
$this->resolvedGroups[$field] = $groups;
131132
$fieldFormConstraint = new Form();
133+
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
132134
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
133135
}
134136
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Form\Test\ForwardCompatTestTrait;
2222
use Symfony\Component\OptionsResolver\OptionsResolver;
2323
use Symfony\Component\Validator\Constraints\Collection;
24+
use Symfony\Component\Validator\Constraints\Expression;
2425
use Symfony\Component\Validator\Constraints\GroupSequence;
2526
use Symfony\Component\Validator\Constraints\Length;
2627
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -283,6 +284,51 @@ public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSe
283284
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
284285
$this->assertSame('children[field1].data', $violations[0]->getPropertyPath());
285286
}
287+
288+
public function testContextIsPopulatedWithFormBeingValidated()
289+
{
290+
$form = $this->formFactory->create(FormType::class)
291+
->add('field1', null, [
292+
'constraints' => [new Expression([
293+
'expression' => '!this.getParent().get("field2").getData()',
294+
])],
295+
])
296+
->add('field2')
297+
;
298+
299+
$form->submit([
300+
'field1' => '',
301+
'field2' => '',
302+
]);
303+
304+
$violations = $this->validator->validate($form);
305+
306+
$this->assertCount(0, $violations);
307+
}
308+
309+
public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence()
310+
{
311+
$form = $this->formFactory->create(FormType::class, null, [
312+
'validation_groups' => new GroupSequence(['group1']),
313+
])
314+
->add('field1', null, [
315+
'constraints' => [new Expression([
316+
'expression' => '!this.getParent().get("field2").getData()',
317+
'groups' => ['group1'],
318+
])],
319+
])
320+
->add('field2')
321+
;
322+
323+
$form->submit([
324+
'field1' => '',
325+
'field2' => '',
326+
]);
327+
328+
$violations = $this->validator->validate($form);
329+
330+
$this->assertCount(0, $violations);
331+
}
286332
}
287333

288334
class Foo

src/Symfony/Component/Form/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
},
2727
"require-dev": {
2828
"doctrine/collections": "~1.0",
29-
"symfony/validator": "^3.2.5|~4.0",
29+
"symfony/validator": "^3.4.3|^4.0.3",
3030
"symfony/dependency-injection": "~3.3|~4.0",
3131
"symfony/config": "~2.7|~3.0|~4.0",
32+
"symfony/expression-language": "~3.4|~4.0",
3233
"symfony/http-foundation": "~2.8|~3.0|~4.0",
3334
"symfony/http-kernel": "^3.3.5|~4.0",
3435
"symfony/security-csrf": "^2.8.31|^3.3.13|~4.0",

0 commit comments

Comments
 (0)