1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #34488 -- Made ClearableFileInput preserve "Clear" checked attribute when form is invalid.

This commit is contained in:
Marcelo Galigniana
2023-04-20 00:40:56 -03:00
committed by Mariusz Felisiak
parent fb535e0a90
commit 8a6c0203c4
4 changed files with 77 additions and 33 deletions

View File

@@ -433,6 +433,7 @@ class ClearableFileInput(FileInput):
initial_text = _("Currently")
input_text = _("Change")
template_name = "django/forms/widgets/clearable_file_input.html"
checked = False
def clear_checkbox_name(self, name):
"""
@@ -475,10 +476,12 @@ class ClearableFileInput(FileInput):
}
)
context["widget"]["attrs"].setdefault("disabled", False)
context["widget"]["attrs"]["checked"] = self.checked
return context
def value_from_datadict(self, data, files, name):
upload = super().value_from_datadict(data, files, name)
self.checked = self.clear_checkbox_name(name) in data
if not self.is_required and CheckboxInput().value_from_datadict(
data, files, self.clear_checkbox_name(name)
):