Skip to content

Commit

Permalink
bug #45702 [Form] Fix the usage of the Valid constraints in array-bas…
Browse files Browse the repository at this point in the history
…ed forms (stof)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] Fix the usage of the Valid constraints in array-based forms

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

This is a bug introduced in #39333. When wanting to exclude scalar forms, it also excluded array forms.
The code now uses the same `\is_object($data) || \is_array($data)` condition for `Valid` constraint than the condition it uses as part of the `$validateDataGraph` condition.

The new tests reflects my own use case: an unmapped collection field, were we want to run the validation on that subtree (which is not traversed for other reasons due to being unmapped, and so we need `Valid`). This was working fine in older versions of Symfony, but the validation was silently skipped in 4.4.18+.

Commits
-------

542c2fb Fix the usage of the Valid constraints in array-based forms
  • Loading branch information
stof committed Mar 11, 2022
2 parents ab136f0 + 542c2fb commit a2fc328
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 a2fc328

Please sign in to comment.