Skip to content

Commit de1ebfc

Browse files
committed
Fix the usage of the Valid constraints in array-based forms
1 parent ab136f0 commit de1ebfc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function validate($form, Constraint $formConstraint)
110110
foreach ($constraints as $constraint) {
111111
// For the "Valid" constraint, validate the data in all groups
112112
if ($constraint instanceof Valid) {
113-
if (\is_object($data)) {
113+
if (\is_object($data) || \is_array($data)) {
114114
$validator->atPath('data')->validate($data, $constraint, $groups);
115115
}
116116

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorFunctionalTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\AbstractType;
1616
use Symfony\Component\Form\CallbackTransformer;
1717
use Symfony\Component\Form\Exception\TransformationFailedException;
18+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
1819
use Symfony\Component\Form\Extension\Core\Type\DateType;
1920
use Symfony\Component\Form\Extension\Core\Type\FormType;
2021
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
@@ -328,6 +329,35 @@ public function testCascadeValidationToChildFormsWithTwoValidConstraints2()
328329
$this->assertSame('children[author].data.email', $violations[1]->getPropertyPath());
329330
}
330331

332+
public function testCascadeValidationToArrayChildForm()
333+
{
334+
$form = $this->formFactory->create(FormType::class, null, [
335+
'data_class' => Review::class,
336+
])
337+
->add('title')
338+
->add('customers', CollectionType::class, [
339+
'mapped' => false,
340+
'entry_type' => CustomerType::class,
341+
'allow_add' => true,
342+
'constraints' => [new Valid()]
343+
]);
344+
345+
$form->submit([
346+
'title' => 'Sample Title',
347+
'customers' => [
348+
['email' => null],
349+
],
350+
]);
351+
352+
$violations = $this->validator->validate($form);
353+
354+
$this->assertCount(2, $violations);
355+
$this->assertSame('This value should not be blank.', $violations[0]->getMessage());
356+
$this->assertSame('data.rating', $violations[0]->getPropertyPath());
357+
$this->assertSame('This value should not be blank.', $violations[1]->getMessage());
358+
$this->assertSame('children[customers].data[0].email', $violations[1]->getPropertyPath());
359+
}
360+
331361
public function testCascadeValidationToChildFormsUsingPropertyPathsValidatedInSequence()
332362
{
333363
$form = $this->formFactory->create(FormType::class, null, [

0 commit comments

Comments
 (0)