Skip to content

Commit

Permalink
Refs #34488 -- Made ClearableFileInput preserve "Clear" checked attri…
Browse files Browse the repository at this point in the history
…bute when form is invalid.
  • Loading branch information
codingjoe authored and felixxm committed Mar 14, 2024
1 parent e690195 commit 3d7235c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}{% if widget.attrs.checked %} checked{% endif %}>
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if widget.is_initial %}{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>{% if not widget.required %}
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}>
<input type="checkbox" name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}"{% if widget.attrs.disabled %} disabled{% endif %}{% if widget.attrs.checked %} checked{% endif %}>
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>{% endif %}<br>
{{ widget.input_text }}:{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
15 changes: 15 additions & 0 deletions tests/forms_tests/widget_tests/test_clearablefileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ def test_render_disabled(self):
),
)

def test_render_checked(self):
self.widget.checked = True
self.check_html(
self.widget,
"myfile",
FakeFieldFile(),
html=(
'Currently: <a href="something">something</a>'
'<input type="checkbox" name="myfile-clear" id="myfile-clear_id" '
"checked>"
'<label for="myfile-clear_id">Clear</label><br>Change: '
'<input type="file" name="myfile" checked>'
),
)

def test_render_no_disabled(self):
class TestForm(Form):
clearable_file = FileField(
Expand Down

0 comments on commit 3d7235c

Please sign in to comment.