propagate validation groups to subforms

This commit is contained in:
Christian Flothmann 2020-10-02 11:42:08 +02:00
parent e2c7c3373d
commit 04f5698e29
1 changed files with 9 additions and 2 deletions

View File

@ -25,6 +25,7 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class FormValidator extends ConstraintValidator
{
private $resolvedGroups;
private $fieldFormConstraints;
/**
* {@inheritdoc}
@ -67,6 +68,7 @@ class FormValidator extends ConstraintValidator
if ($hasChildren && $form->isRoot()) {
$this->resolvedGroups = new \SplObjectStorage();
$this->fieldFormConstraints = [];
}
if ($groups instanceof GroupSequence) {
@ -84,14 +86,16 @@ class FormValidator extends ConstraintValidator
foreach ($form->all() as $field) {
if ($field->isSubmitted()) {
// remember to validate this field is one group only
// remember to validate this field in one group only
// otherwise resolving the groups would reuse the same
// sequence recursively, thus some fields could fail
// in different steps without breaking early enough
$this->resolvedGroups[$field] = (array) $group;
$fieldFormConstraint = new Form();
$fieldFormConstraint->groups = $group;
$this->fieldFormConstraints[] = $fieldFormConstraint;
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint, $group);
}
}
@ -130,6 +134,7 @@ class FormValidator extends ConstraintValidator
if ($field->isSubmitted()) {
$this->resolvedGroups[$field] = $groups;
$fieldFormConstraint = new Form();
$this->fieldFormConstraints[] = $fieldFormConstraint;
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint);
}
@ -139,6 +144,7 @@ class FormValidator extends ConstraintValidator
if ($hasChildren && $form->isRoot()) {
// destroy storage to avoid memory leaks
$this->resolvedGroups = new \SplObjectStorage();
$this->fieldFormConstraints = [];
}
} elseif (!$form->isSynchronized()) {
$childrenSynchronized = true;
@ -149,6 +155,7 @@ class FormValidator extends ConstraintValidator
$childrenSynchronized = false;
$fieldFormConstraint = new Form();
$this->fieldFormConstraints[] = $fieldFormConstraint;
$this->context->setNode($this->context->getValue(), $child, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(sprintf('children[%s]', $child->getName()))->validate($child, $fieldFormConstraint);
}