Skip to content

Commit

Permalink
Fix the usage of the Valid constraints in array-based forms
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Mar 10, 2022
1 parent ab136f0 commit 542c2fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function validate($form, Constraint $formConstraint)
foreach ($constraints as $constraint) {
// For the "Valid" constraint, validate the data in all groups
if ($constraint instanceof Valid) {
if (\is_object($data)) {
if (\is_object($data) || \is_array($data)) {
$validator->atPath('data')->validate($data, $constraint, $groups);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
Expand Down Expand Up @@ -328,6 +329,35 @@ public function testCascadeValidationToChildFormsWithTwoValidConstraints2()
$this->assertSame('children[author].data.email', $violations[1]->getPropertyPath());
}

public function testCascadeValidationToArrayChildForm()
{
$form = $this->formFactory->create(FormType::class, null, [
'data_class' => Review::class,
])
->add('title')
->add('customers', CollectionType::class, [
'mapped' => false,
'entry_type' => CustomerType::class,
'allow_add' => true,
'constraints' => [new Valid()],
]);

$form->submit([
'title' => 'Sample Title',
'customers' => [
['email' => null],
],
]);

$violations = $this->validator->validate($form);

$this->assertCount(2, $violations);
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
$this->assertSame('data.rating', $violations[0]->getPropertyPath());
$this->assertSame('This value should not be blank.', $violations[1]->getMessage());
$this->assertSame('children[customers].data[0].email', $violations[1]->getPropertyPath());
}

public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
{
$form = $this->formFactory->create(FormType::class, null, [
Expand Down

0 comments on commit 542c2fb

Please sign in to comment.