bug #42049 [TwigBridge] do not render the same label id attribute twice (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[TwigBridge] do not render the same label id attribute twice

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #40562
| License       | MIT
| Doc PR        |

Commits
-------

ceee81e33d do not render the same label id attribute twice
This commit is contained in:
Fabien Potencier 2021-07-10 18:05:06 +02:00
commit 7385bc88d4
2 changed files with 19 additions and 1 deletions

View File

@ -122,7 +122,7 @@
<{{ element|default('div') }} class="custom-file">
{%- set type = type|default('file') -%}
{{- block('form_widget_simple') -}}
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim }) -%}
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim })|filter((value, key) => key != 'id') -%}
<label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
{%- if attr.placeholder is defined and attr.placeholder is not none -%}
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}

View File

@ -1088,6 +1088,24 @@ abstract class AbstractBootstrap4LayoutTest extends AbstractBootstrap3LayoutTest
);
}
public function testFileLabelIdNotDuplicated()
{
$form = $this->factory->createNamed('name', FileType::class);
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'n/a', 'attr' => ['class' => 'my&class form-control-file'], 'label_attr' => ['id' => 'label-id']],
'/div
[@class="custom-file"]
[
./input
[@type="file"]
[@name="name"]
/following-sibling::label
[@for="name"][not(@id)]
]
'
);
}
public function testFileWithPlaceholder()
{
$form = $this->factory->createNamed('name', FileType::class);