do not render the same label id attribute twice

This commit is contained in:
Christian Flothmann 2021-07-10 14:16:00 +02:00
parent fa182e613a
commit ceee81e33d
2 changed files with 19 additions and 1 deletions

View File

@ -122,7 +122,7 @@
<{{ element|default('div') }} class="custom-file"> <{{ element|default('div') }} class="custom-file">
{%- set type = type|default('file') -%} {%- set type = type|default('file') -%}
{{- block('form_widget_simple') -}} {{- 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 %}> <label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
{%- if attr.placeholder is defined and attr.placeholder is not none -%} {%- if attr.placeholder is defined and attr.placeholder is not none -%}
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}} {{- 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() public function testFileWithPlaceholder()
{ {
$form = $this->factory->createNamed('name', FileType::class); $form = $this->factory->createNamed('name', FileType::class);