Skip to content
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

Add file field support #53

Merged
merged 5 commits into from
Jun 1, 2021
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
9 changes: 5 additions & 4 deletions crispy_bootstrap5/templates/bootstrap5/field.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
{% include 'bootstrap5/layout/help_text_and_errors.html' %}
{% else %}
{% if field_class %}<div class="{{ field_class }}">{% endif %}
{% if field|is_file and field|is_clearable_file %}
{# clearable files are more complex, potentially need a checkbox -- let Django render these for now #}
{% crispy_field field %}
{% if field|is_file %}
{% include 'bootstrap5/layout/field_file.html' %}
{% elif field|is_select %}
{% if field.errors %}
{% crispy_field field 'class' 'form-select is-invalid' %}
Expand All @@ -51,7 +50,9 @@
{% else %}
{% crispy_field field 'class' 'form-control' %}
{% endif %}
{% include 'bootstrap5/layout/help_text_and_errors.html' %}
{% if not field|is_file %}
{% include 'bootstrap5/layout/help_text_and_errors.html' %}
{% endif %}
{% if field_class %}</div>{% endif %}
{% endif %}
{% endif %}
Expand Down
28 changes: 28 additions & 0 deletions crispy_bootstrap5/templates/bootstrap5/layout/field_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% load crispy_forms_field %}

<div class="{{ field_class }} mb-2">
{% for widget in field.subwidgets %}
{% if widget.data.is_initial %}
<div class="input-group mb-2">
<span class="input-group-text">{{ widget.data.initial_text }}</span>
<div class="form-control d-flex h-auto">
<span class="text-break" style="flex-grow:1;min-width:0">
<a href="{{ field.value.url }}">{{ field.value.name }}</a>
</span>
{% if not widget.data.required %}
<span class="align-self-center ml-2">
<span class="form-check">
<input type="checkbox" name="{{ widget.data.checkbox_name }}" id="{{ widget.data.checkbox_id }}" class="form-check-input"{% if field.field.disabled %} disabled{% endif %} >
<label class="form-check-label mb-0" for="{{ widget.data.checkbox_id }}">{{ widget.data.clear_checkbox_label }}</label>
</span>
</span>
{% endif %}
</div>
</div>
{% endif %}
<div{% if field.errors %} class="is-invalid"{%endif%}>
<input type="{{ widget.data.type }}" name="{{ widget.data.name }}" class="form-control{% if widget.data.attrs.class %} {{ widget.data.attrs.class }}{% endif %}{% if field.errors %} is-invalid{%endif%}"{% if field.field.disabled %} disabled{% endif %}{% for name, value in widget.data.attrs.items %}{% if value is not False and name != 'class' %} {{ name }}{% if value is not True %}="{{ value|stringformat:'s' }}"{% endif %}{% endif %}{% endfor %}>
{% include 'bootstrap5/layout/help_text_and_errors.html' %}
</div>
{% endfor %}
</div>
5 changes: 5 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ class FileForm(forms.Form):
)


class FileFormRequired(forms.Form):
file_field = forms.FileField(required=True, widget=forms.FileInput)
clearable_file = forms.FileField(required=True, widget=forms.ClearableFileInput)


class InputsForm(forms.Form):
choices = ((1, "Option one"), (2, "Option two"), (3, "Option three"))
text_input = forms.CharField()
Expand Down
24 changes: 24 additions & 0 deletions tests/results/test_clearable_file_field.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<form method="post" enctype="multipart/form-data">
<div id="div_id_clearable_file" class="mb-3">
<label for="id_clearable_file" class="form-label">Clearable file</label>
<div class=" mb-2">
<div class="input-group mb-2">
<span class="input-group-text">Currently</span>
<div class="form-control d-flex h-auto">
<span class="text-break" style="flex-grow:1;min-width:0">
<a href="something"></a>
</span>
<span class="align-self-center ml-2">
<span class="form-check">
<input type="checkbox" name="clearable_file-clear" id="clearable_file-clear_id" class="form-check-input" >
<label class="form-check-label mb-0" for="clearable_file-clear_id">Clear</label>
</span>
</span>
</div>
</div>
<div>
<input type="file" name="clearable_file" class="form-control" id="id_clearable_file">
</div>
</div>
</div>
</form>
16 changes: 16 additions & 0 deletions tests/results/test_clearable_file_field_failing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form method="post" enctype="multipart/form-data">
<div id="div_id_clearable_file" class="mb-3">
<label for="id_clearable_file" class="form-label requiredField">
Clearable file
<span class="asteriskField">*</span>
</label>
<div class=" mb-2">
<div class="is-invalid">
<input type="file" name="clearable_file" class="form-control is-invalid" id="id_clearable_file" required>
<span id="error_1_id_clearable_file" class="invalid-feedback">
<strong>This field is required.</strong>
</span>
</div>
</div>
</div>
</form>
13 changes: 13 additions & 0 deletions tests/results/test_file_field.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<form method="post" enctype="multipart/form-data">
<div id="div_id_file_field" class="mb-3">
<label for="id_file_field" class="form-label requiredField">
File field
<span class="asteriskField">*</span>
</label>
<div class=" mb-2">
<div>
<input type="file" name="file_field" class="form-control" id="id_file_field" required>
</div>
</div>
</div>
</form>
16 changes: 16 additions & 0 deletions tests/results/test_file_field_failing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form method="post" enctype="multipart/form-data">
<div id="div_id_file_field" class="mb-3">
<label for="id_file_field" class="form-label requiredField">
File field
<span class="asteriskField">*</span>
</label>
<div class=" mb-2">
<div class="is-invalid">
<input type="file" name="file_field" class="form-control is-invalid" id="id_file_field" required>
<span id="error_1_id_file_field" class="invalid-feedback">
<strong>This field is required.</strong>
</span>
</div>
</div>
</div>
</form>
26 changes: 15 additions & 11 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
CrispyEmptyChoiceTestModel,
CrispyTestModel,
FileForm,
FileFormRequired,
InputsForm,
SampleForm,
SampleForm2,
Expand Down Expand Up @@ -570,19 +571,22 @@ def test_file_field():
form = FileForm()
form.helper = FormHelper()
form.helper.layout = Layout("clearable_file")
html = render_crispy_form(form)
assert (
'input type="checkbox" name="clearable_file-clear" id="clearable_file-clear_id'
in html
)
assert '<input type="file" name="clearable_file" class="clearablefileinput"' in html

assert parse_form(form) == parse_expected("test_clearable_file_field.html")

form.helper.layout = Layout("file_field")
html = render_crispy_form(form)
assert (
'<input type="file" name="file_field" class="fileinput fileUpload '
'form-control" required id="id_file_field">' in html
)

assert parse_form(form) == parse_expected("test_file_field.html")

form = FileFormRequired({})
form.helper = FormHelper()
form.helper.layout = Layout("clearable_file")

assert parse_form(form) == parse_expected("test_clearable_file_field_failing.html")

form.helper.layout = Layout("file_field")

assert parse_form(form) == parse_expected("test_file_field_failing.html")


def test_row():
Expand Down