Skip to content

Commit f24a583

Browse files
ttskchnicolas-grekas
authored andcommitted
[TwigBridge] Improve form_errors of bootstrap5 form theme
1 parent 3dae0d0 commit f24a583

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

Resources/views/Form/bootstrap_5_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353
{%- block form_errors -%}
354354
{%- if errors|length > 0 -%}
355355
{%- for error in errors -%}
356-
<div class="invalid-feedback d-block">{{ error.message }}</div>
356+
<div class="{% if form is not rootform %}invalid-feedback{% else %}alert alert-danger{% endif %} d-block">{{ error.message }}</div>
357357
{%- endfor -%}
358358
{%- endif %}
359359
{%- endblock form_errors %}

Tests/Extension/AbstractBootstrap5HorizontalLayoutTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ abstract class AbstractBootstrap5HorizontalLayoutTest extends AbstractBootstrap5
2828
{
2929
public function testRow()
3030
{
31-
$form = $this->factory->createNamed('name', TextType::class);
32-
$form->addError(new FormError('[trans]Error![/trans]'));
33-
$html = $this->renderRow($form->createView());
31+
$form = $this->factory->createNamed('')->add('name', TextType::class);
32+
$form->get('name')->addError(new FormError('[trans]Error![/trans]'));
33+
$html = $this->renderRow($form->get('name')->createView());
3434

3535
$this->assertMatchesXpath($html,
3636
'/div
@@ -55,9 +55,9 @@ public function testRow()
5555

5656
public function testRowWithCustomClass()
5757
{
58-
$form = $this->factory->createNamed('name', TextType::class);
59-
$form->addError(new FormError('[trans]Error![/trans]'));
60-
$html = $this->renderRow($form->createView(), [
58+
$form = $this->factory->createNamed('')->add('name', TextType::class);
59+
$form->get('name')->addError(new FormError('[trans]Error![/trans]'));
60+
$html = $this->renderRow($form->get('name')->createView(), [
6161
'row_attr' => [
6262
'class' => 'mb-5',
6363
],

Tests/Extension/AbstractBootstrap5LayoutTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ abstract class AbstractBootstrap5LayoutTest extends AbstractBootstrap4LayoutTest
4040
{
4141
public function testRow()
4242
{
43-
$form = $this->factory->createNamed('name', TextType::class);
44-
$form->addError(new FormError('[trans]Error![/trans]'));
45-
$html = $this->renderRow($form->createView());
43+
$form = $this->factory->createNamed('')->add('name', TextType::class);
44+
$form->get('name')->addError(new FormError('[trans]Error![/trans]'));
45+
$html = $this->renderRow($form->get('name')->createView());
4646

4747
$this->assertMatchesXpath($html,
4848
'/div
@@ -61,9 +61,9 @@ public function testRow()
6161

6262
public function testRowWithCustomClass()
6363
{
64-
$form = $this->factory->createNamed('name', TextType::class);
65-
$form->addError(new FormError('[trans]Error![/trans]'));
66-
$html = $this->renderRow($form->createView(), [
64+
$form = $this->factory->createNamed('')->add('name', TextType::class);
65+
$form->get('name')->addError(new FormError('[trans]Error![/trans]'));
66+
$html = $this->renderRow($form->get('name')->createView(), [
6767
'row_attr' => [
6868
'class' => 'mb-5',
6969
],
@@ -309,11 +309,34 @@ public function testHelpHtmlIsTrue()
309309

310310
public function testErrors()
311311
{
312-
$form = $this->factory->createNamed('name', TextType::class);
312+
self::markTestSkipped('This method has been split into testRootErrors() and testRowErrors().');
313+
}
314+
315+
public function testRootErrors()
316+
{
317+
$form = $this->factory->createNamed('');
313318
$form->addError(new FormError('[trans]Error 1[/trans]'));
314319
$form->addError(new FormError('[trans]Error 2[/trans]'));
315320
$html = $this->renderErrors($form->createView());
316321

322+
$this->assertMatchesXpath($html,
323+
'/div
324+
[@class="alert alert-danger d-block"]
325+
[.="[trans]Error 1[/trans]"]
326+
/following-sibling::div
327+
[@class="alert alert-danger d-block"]
328+
[.="[trans]Error 2[/trans]"]
329+
'
330+
);
331+
}
332+
333+
public function testRowErrors()
334+
{
335+
$form = $this->factory->createNamed('')->add('name', TextType::class);
336+
$form->get('name')->addError(new FormError('[trans]Error 1[/trans]'));
337+
$form->get('name')->addError(new FormError('[trans]Error 2[/trans]'));
338+
$html = $this->renderErrors($form->get('name')->createView());
339+
317340
$this->assertMatchesXpath($html,
318341
'/div
319342
[@class="invalid-feedback d-block"]

0 commit comments

Comments
 (0)