Skip to content

[Form] Improve rendering of file field in bootstrap 4 #27919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@
</div>
{%- endblock percent_widget %}

{% block file_widget -%}
<div class="form-group">
<{{ element|default('div') }} class="custom-file">
{%- set type = type|default('file') -%}
{{- block('form_widget_simple') -}}
<label for="{{ form.vars.id }}" class="custom-file-label">Choose File</label>
</{{ element|default('div') }}>
</div>
{% endblock %}

{% block form_widget_simple -%}
{% if type is not defined or type != 'hidden' %}
{%- set attr = attr|merge({class: (attr.class|default('') ~ (type|default('') == 'file' ? ' custom-file-input' : ' form-control'))|trim}) -%}
Expand Down Expand Up @@ -186,8 +196,6 @@
{%- if compound is defined and compound -%}
{%- set element = 'legend' -%}
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
{% elseif type is defined and type == 'file' %}
{%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default('') ~ ' custom-file-label')|trim}) -%}
{%- else -%}
{%- set label_attr = label_attr|merge({for: id}) -%}
{%- endif -%}
Expand Down Expand Up @@ -269,15 +277,6 @@
</{{ element|default('div') }}>
{%- endblock form_row %}

{% block file_row -%}
<div class="form-group">
<{{ element|default('div') }} class="custom-file">
{{- form_widget(form) -}}
{{- form_label(form) -}}
</{{ element|default('div') }}>
</div>
{% endblock %}

{# Errors #}

{% block form_errors -%}
Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap4LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,20 @@ public function testFile()
{
$form = $this->factory->createNamed('name', FileType::class);

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class form-control-file')),
'/input
[@type="file"]
$this->assertWidgetMatchesXpath($form->createView(), array('id' => 'nope', 'attr' => array('class' => 'my&class form-control-file')),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this id-thing here is weird, but the check doesn't make sense for this file type (because of the special structure), I guess.

'/div
[@class="form-group"]
[
./div
[@class="custom-file"]
[
./input
[@type="file"]
[@name="name"]
/following-sibling::label
[@for="name"]
]
]
'
);
}
Expand Down