Skip to content

Commit 8ee6c72

Browse files
authored
fix(form): Error messages displayed twice with form type "sonata_type_native_collection" or "sonata_type_immutable_array" when "error_bubbling" sets to false (#8335)
1 parent 58ca714 commit 8ee6c72

File tree

4 files changed

+106
-6
lines changed

4 files changed

+106
-6
lines changed

src/Resources/views/Form/form_admin_fields.html.twig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ file that was distributed with this source code.
402402
{% set allow_delete = allow_delete_backup %}
403403
{% endif %}
404404
<div {{ block('widget_container_attributes') }}>
405-
{{ form_errors(form) }}
406405
{% for child in form %}
407406
{{ block('sonata_type_native_collection_widget_row') }}
408407
{% endfor %}
@@ -419,8 +418,6 @@ file that was distributed with this source code.
419418
<div {{ block('widget_container_attributes') }}>
420419
{{ form_help(form) }}
421420

422-
{{ form_errors(form) }}
423-
424421
{% for key, child in form %}
425422
{{ block('sonata_type_immutable_array_widget_row') }}
426423
{% endfor %}

tests/App/Admin/FooAdmin.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
use Sonata\AdminBundle\Datagrid\ListMapper;
2020
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
2121
use Sonata\AdminBundle\Form\FormMapper;
22+
use Sonata\AdminBundle\Form\Type\CollectionType;
2223
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
2324
use Sonata\AdminBundle\Form\Type\TemplateType;
2425
use Sonata\AdminBundle\Show\ShowMapper;
2526
use Sonata\AdminBundle\Tests\App\Model\Bar;
2627
use Sonata\AdminBundle\Tests\App\Model\Foo;
2728
use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchOtherController;
29+
use Sonata\Form\Type\ImmutableArrayType;
2830
use Symfony\Component\Form\Extension\Core\Type\TextType;
31+
use Symfony\Component\Validator\Constraints\Collection;
32+
use Symfony\Component\Validator\Constraints\Count;
33+
use Symfony\Component\Validator\Constraints\NotBlank;
2934

3035
/**
3136
* @phpstan-extends AbstractAdmin<Foo>
@@ -65,6 +70,45 @@ protected function configureFormFields(FormMapper $form): void
6570
[
6671
'admin_code' => 'sonata_bar_admin',
6772
]
73+
)
74+
->add(
75+
'elements',
76+
ImmutableArrayType::class,
77+
[
78+
'help' => 'elements help message',
79+
'error_bubbling' => false,
80+
'constraints' => [
81+
new Collection([
82+
'fields' => [
83+
'elements_item' => new NotBlank(),
84+
'missing_field' => new NotBlank(),
85+
],
86+
'allowMissingFields' => false,
87+
]),
88+
],
89+
'keys' => [
90+
[
91+
'elements_item',
92+
TextType::class,
93+
[
94+
'help' => 'elements_item help message',
95+
],
96+
],
97+
],
98+
],
99+
)
100+
->add(
101+
'collection',
102+
CollectionType::class,
103+
[
104+
'error_bubbling' => false,
105+
'constraints' => [
106+
new Count([
107+
'min' => 2,
108+
]),
109+
],
110+
'entry_type' => TextType::class,
111+
],
68112
);
69113
}
70114

tests/App/Model/Foo.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ final class Foo implements EntityInterface
1818
private ?Bar $referenced;
1919

2020
/**
21-
* @param string[] $elements
21+
* @var array<string, string>
2222
*/
23+
private array $elements = [];
24+
25+
/**
26+
* @var string[]
27+
*/
28+
private array $collection = [];
29+
2330
public function __construct(
2431
private string $id,
2532
private string $name,
26-
private array $elements = [],
2733
) {
2834
$this->referenced = null;
2935
}
@@ -49,10 +55,34 @@ public function getReferenced(): ?Bar
4955
}
5056

5157
/**
52-
* @return string[]
58+
* @param array<string, string> $elements
59+
*/
60+
public function setElements(array $elements): void
61+
{
62+
$this->elements = $elements;
63+
}
64+
65+
/**
66+
* @return array<string, string>
5367
*/
5468
public function getElements(): array
5569
{
5670
return $this->elements;
5771
}
72+
73+
/**
74+
* @param string[] $collection
75+
*/
76+
public function setCollection(array $collection): void
77+
{
78+
$this->collection = $collection;
79+
}
80+
81+
/**
82+
* @return string[]
83+
*/
84+
public function getCollection(): array
85+
{
86+
return $this->collection;
87+
}
5888
}

tests/Functional/Controller/CRUDControllerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ public function testCreate(): void
4747
);
4848
}
4949

50+
public function testImmutableArrayErrorMessageIsDisplayOnce(): void
51+
{
52+
$client = static::createClient();
53+
$client->followRedirects();
54+
$client->setMaxRedirects(1);
55+
$client->request(Request::METHOD_GET, '/admin/tests/app/foo/create', ['with_form_errors' => '1']);
56+
$crawler = $client->submitForm('Create', []);
57+
file_put_contents('tmp.html', $crawler->html());
58+
static::assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
59+
static::assertCount(
60+
1,
61+
$crawler->filter('.sonata-ba-field li:contains("This field is missing.")')
62+
);
63+
}
64+
65+
public function testCollectionErrorMessageIsDisplayOnce(): void
66+
{
67+
$client = static::createClient();
68+
$client->followRedirects();
69+
$client->setMaxRedirects(1);
70+
$client->request(Request::METHOD_GET, '/admin/tests/app/foo/create', ['with_form_errors' => '1']);
71+
$crawler = $client->submitForm('Create', []);
72+
static::assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
73+
static::assertCount(
74+
1,
75+
$crawler->filter('.sonata-ba-field li:contains("This collection should contain 2 elements or more.")')
76+
);
77+
}
78+
5079
/**
5180
* https://github.com/sonata-project/SonataAdminBundle/issues/6904.
5281
*/

0 commit comments

Comments
 (0)