Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] switch the context when validating nested forms #37103

Merged
merged 1 commit into from Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to get #25529 which prevents the deps=low build to fail

"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