Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a2fc328

Browse filesBrowse files
committed
bug #45702 [Form] Fix the usage of the Valid constraints in array-based 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
2 parents ab136f0 + 542c2fb commit a2fc328
Copy full SHA for a2fc328

File tree

Expand file treeCollapse file tree

2 files changed

+31
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php
+1-1Lines changed: 1 addition & 1 deletion
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorFunctionalTest.php
+30Lines changed: 30 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.