Skip to content

Commit 32ad5d9

Browse files
Jannik Zschieschefabpot
authored andcommitted
[Form] Improve rendering of file field in bootstrap 4
1 parent a552e84 commit 32ad5d9

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@
114114
</div>
115115
{%- endblock percent_widget %}
116116

117+
{% block file_widget -%}
118+
<div class="form-group">
119+
<{{ element|default('div') }} class="custom-file">
120+
{%- set type = type|default('file') -%}
121+
{{- block('form_widget_simple') -}}
122+
<label for="{{ form.vars.id }}" class="custom-file-label">Choose File</label>
123+
</{{ element|default('div') }}>
124+
</div>
125+
{% endblock %}
126+
117127
{% block form_widget_simple -%}
118128
{% if type is not defined or type != 'hidden' %}
119129
{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' custom-file-input' : ' form-control'))|trim}) -%}
@@ -186,8 +196,6 @@
186196
{%- if compound is defined and compound -%}
187197
{%- set element = 'legend' -%}
188198
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
189-
{% elseif type is defined and type == 'file' %}
190-
{%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default('') ~ ' custom-file-label')|trim}) -%}
191199
{%- else -%}
192200
{%- set label_attr = label_attr|merge({for: id}) -%}
193201
{%- endif -%}
@@ -269,15 +277,6 @@
269277
</{{ element|default('div') }}>
270278
{%- endblock form_row %}
271279

272-
{% block file_row -%}
273-
<div class="form-group">
274-
<{{ element|default('div') }} class="custom-file">
275-
{{- form_widget(form) -}}
276-
{{- form_label(form) -}}
277-
</{{ element|default('div') }}>
278-
</div>
279-
{% endblock %}
280-
281280
{# Errors #}
282281

283282
{% block form_errors -%}

src/Symfony/Component/Form/Tests/AbstractBootstrap4LayoutTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,20 @@ public function testFile()
940940
{
941941
$form = $this->factory->createNamed('name', FileType::class);
942942

943-
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class form-control-file')),
944-
'/input
945-
[@type="file"]
943+
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'nope', 'attr' => array('class' => 'my&class form-control-file')),
944+
'/div
945+
[@class="form-group"]
946+
[
947+
./div
948+
[@class="custom-file"]
949+
[
950+
./input
951+
[@type="file"]
952+
[@name="name"]
953+
/following-sibling::label
954+
[@for="name"]
955+
]
956+
]
946957
'
947958
);
948959
}

0 commit comments

Comments
 (0)