Skip to content

Commit

Permalink
bug #37103 [Form] switch the context when validating nested forms (xa…
Browse files Browse the repository at this point in the history
…bbuh)

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 #37072
| License       | MIT
| Doc PR        |

Commits
-------

38135de switch the context when validating nested forms
  • Loading branch information
fabpot committed Jun 12, 2020
2 parents 816b6ea + 38135de commit 1f83212
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Expand Up @@ -90,6 +90,7 @@ public function validate($form, Constraint $formConstraint)
// in different steps without breaking early enough
$this->resolvedGroups[$field] = (array) $group;
$fieldFormConstraint = new Form();
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
}
}
Expand Down Expand Up @@ -129,6 +130,7 @@ public function validate($form, Constraint $formConstraint)
if ($field->isSubmitted()) {
$this->resolvedGroups[$field] = $groups;
$fieldFormConstraint = new Form();
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
}
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Form\Test\ForwardCompatTestTrait;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
Expand Down Expand Up @@ -283,6 +284,51 @@ public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSe
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
$this->assertSame('children[field1].data', $violations[0]->getPropertyPath());
}

public function testContextIsPopulatedWithFormBeingValidated()
{
$form = $this->formFactory->create(FormType::class)
->add('field1', null, [
'constraints' => [new Expression([
'expression' => '!this.getParent().get("field2").getData()',
])],
])
->add('field2')
;

$form->submit([
'field1' => '',
'field2' => '',
]);

$violations = $this->validator->validate($form);

$this->assertCount(0, $violations);
}

public function testContextIsPopulatedWithFormBeingValidatedUsingGroupSequence()
{
$form = $this->formFactory->create(FormType::class, null, [
'validation_groups' => new GroupSequence(['group1']),
])
->add('field1', null, [
'constraints' => [new Expression([
'expression' => '!this.getParent().get("field2").getData()',
'groups' => ['group1'],
])],
])
->add('field2')
;

$form->submit([
'field1' => '',
'field2' => '',
]);

$violations = $this->validator->validate($form);

$this->assertCount(0, $violations);
}
}

class Foo
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Form/composer.json
Expand Up @@ -26,9 +26,10 @@
},
"require-dev": {
"doctrine/collections": "~1.0",
"symfony/validator": "^3.2.5|~4.0",
"symfony/validator": "^3.4.3|^4.0.3",
"symfony/dependency-injection": "~3.3|~4.0",
"symfony/config": "~2.7|~3.0|~4.0",
"symfony/expression-language": "~3.4|~4.0",
"symfony/http-foundation": "~2.8|~3.0|~4.0",
"symfony/http-kernel": "^3.3.5|~4.0",
"symfony/security-csrf": "^2.8.31|^3.3.13|~4.0",
Expand Down

0 comments on commit 1f83212

Please sign in to comment.