8000 minor #49243 [TwigBridge] Improve form_errors of bootstrap5 form them… · symfony/symfony@d0ef4f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0ef4f0

Browse files
minor #49243 [TwigBridge] Improve form_errors of bootstrap5 form theme (ttskch)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [TwigBridge] Improve form_errors of bootstrap5 form theme | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR improves output of `form_errors()` for the root form with Bootstrap 5 form theme. ### Example <details> <summary>Usage code</summary> ```php public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('text', TextType::class, [ 'constraints' => new Assert\NotBlank(), ]) ->add('textarea', TextareaType::class, [ 'attr' => [ 'rows' => 3, ], 'constraints' => new Assert\NotBlank(), ]) ->add('select', ChoiceType::class, [ 'choices' => [ 'Alice' => 'Alice', 'Bob' => 'Bob', 'Charlie' => 'Charlie', ], 'constraints' => new Assert\Choice(choices: ['Dave']), ]) ->add('selectMultiple', ChoiceType::class, [ 'multiple' => true, 'choices' => [ 'Alice' => 'Alice', 'Bob' => 'Bob', 'Charlie' => 'Charlie', ], 'constraints' => new Assert\Choice(choices: ['Dave']), ]) ->add('radio', ChoiceType::class, [ 'expanded' => true, 'choices' => [ 'Alice' => 'Alice', 'Bob' => 'Bob', 'Charlie' => 'Charlie', ], 'constraints' => new Assert\Choice(choices: ['Dave']), ]) ->add('checkbox', ChoiceType::class, [ 'expanded' => true, 'multiple' => true, 'choices' => [ 'Alice' => 'Alice', 'Bob' => 'Bob', 'Charlie' => 'Charlie', ], 'constraints' => new Assert\Choice(choices: ['Dave']), ]) ->add('singleCheckbox', CheckboxType::class, [ 'constraints' => new Assert\NotBlank(), ]) ; } ``` </details> #### Before <img width="1343" alt="image" src="https://user-images.githubusercontent.com/4360663/216811963-ea66cdfa-2080-4ee0-aaef-433f3276843b.png"> #### After <img width="1340" alt="image" src="https://user-images.githubusercontent.com/4360663/216811972-54b0666b-ccf6-4ecd-9435-f24dfed96447.png"> Commits ------- f4c546d [TwigBridge] Improve form_errors of bootstrap5 form theme
2 parents 8c48f32 + f4c546d commit d0ef4f0

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/Symfony/Bridge/Twig/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 %}

src/Symfony/Bridge/Twig/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
],

src/Symfony/Bridge/Twig/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)
0