Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
propagate validation groups to subforms
  • Loading branch information
xabbuh committed Oct 2, 2020
commit 04f5698e2908efe4881629f7afae79c02acfedc8
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class FormValidator extends ConstraintValidator
{
private $resolvedGroups;
private $fieldFormConstraints;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -67,6 +68,7 @@ public function validate($form, Constraint $formConstraint)

if ($hasChildren && $form->isRoot()) {
$this->resolvedGroups = new \SplObjectStorage();
$this->fieldFormConstraints = [];
}

if ($groups instanceof GroupSequence) {
Expand All @@ -84,14 +86,16 @@ public function validate($form, Constraint $formConstraint)

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);
}
}

Expand Down Expand Up @@ -130,6 +134,7 @@ public function validate($form, Constraint $formConstraint)
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);
}
Expand All @@ -139,6 +144,7 @@ public function validate($form, Constraint $formConstraint)
if ($hasChildren && $form->isRoot()) {
// destroy storage to avoid memory leaks
$this->resolvedGroups = new \SplObjectStorage();
$this->fieldFormConstraints = [];
}
} elseif (!$form->isSynchronized()) {
$childrenSynchronized = true;
Expand All @@ -149,6 +155,7 @@ public function validate($form, Constraint $formConstraint)
$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);
}
Expand Down