Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Apr 15, 2020
1 parent ac17db3 commit 9aa6963
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Expand Up @@ -66,7 +66,7 @@ public function validate($form, Constraint $formConstraint)
if ($groups instanceof GroupSequence) {
// Validate the data, the form AND nested fields in sequence
$violationsCount = $this->context->getViolations()->count();
$fieldPropertyPath = \is_object($data) ? 'data.%s' : '%s';
$fieldPropertyPath = \is_object($data) ? 'children[%s]' : 'children%s';
$hasChildren = $form->count() > 0;
$this->resolvedGroups = $hasChildren ? new \SplObjectStorage() : null;

Expand All @@ -85,7 +85,7 @@ public function validate($form, Constraint $formConstraint)
// 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] = [$group];
$this->resolvedGroups[$field] = (array) $group;
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $formConstraint);
}
}
Expand Down
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -131,6 +130,7 @@ public function testManyFieldsGroupSequenceWithConstraintsOption()

$this->assertCount(1, $errors);
$this->assertInstanceOf(Length::class, $errors[0]->getCause()->getConstraint());
$this->assertSame('children[lastName].data', $errors[0]->getCause()->getPropertyPath());
}

protected function createForm(array $options = [])
Expand Down
Expand Up @@ -113,6 +113,31 @@ public function testFieldsValidateInSequence()
$this->assertInstanceOf(Length::class, $errors[0]->getCause()->getConstraint());
}

public function testFieldsValidateInSequenceWithNestedGroupsArray()
{
$form = $this->createForm(FormType::class, null, [
'validation_groups' => new GroupSequence([['group1', 'group2'], 'group3']),
])
->add('foo', TextType::class, [
'constraints' => [new Length(['min' => 10, 'groups' => ['group1']])],
])
->add('bar', TextType::class, [
'constraints' => [new Length(['min' => 10, 'groups' => ['group2']])],
])
->add('baz', TextType::class, [
'constraints' => [new NotBlank(['groups' => ['group3']])],
])
;

$form->submit(['foo' => 'invalid', 'bar' => 'invalid', 'baz' => null]);

$errors = $form->getErrors(true);

$this->assertCount(2, $errors);
$this->assertInstanceOf(Length::class, $errors[0]->getCause()->getConstraint());
$this->assertInstanceOf(Length::class, $errors[1]->getCause()->getConstraint());
}

private function createForm($type, $data = null, array $options = [])
{
$validator = Validation::createValidatorBuilder()
Expand Down

0 comments on commit 9aa6963

Please sign in to comment.