bug #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 #37072
| License | MIT
| Doc PR |
Commits
-------
38135de549
switch the context when validating nested forms
This commit is contained in:
commit
1f83212541
|
@ -90,6 +90,7 @@ class FormValidator extends ConstraintValidator
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -129,6 +130,7 @@ class FormValidator extends ConstraintValidator
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ use Symfony\Component\Form\FormFactoryBuilder;
|
|||
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;
|
||||
|
@ -283,6 +284,51 @@ class FormValidatorFunctionalTest extends TestCase
|
|||
$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
|
||||
|
|
|
@ -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",
|
||||
|
|
Reference in New Issue