Skip to content

Commit 96f56f5

Browse files
committed
switch the context when validating nested forms
1 parent f87c993 commit 96f56f5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

+2
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

+46
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

0 commit comments

Comments
 (0)