mirror of
https://github.com/django/django.git
synced 2025-11-17 10:43:25 +00:00
Move required attribute logic to FileInput for initial data
Ensure FileInput omits the required attribute when initial data exists, preventing unnecessary validation for already uploaded files. Fixes unexpected required field behavior when editing existing file fields.
This commit is contained in:
parent
53d8646f79
commit
2f4def432d
2 changed files with 9 additions and 3 deletions
|
|
@ -387,6 +387,9 @@ class FileInput(Input):
|
|||
def value_omitted_from_data(self, data, files, name):
|
||||
return name not in files
|
||||
|
||||
def use_required_attribute(self, initial):
|
||||
return super().use_required_attribute(initial) and not initial
|
||||
|
||||
|
||||
FILE_INPUT_CONTRADICTION = object()
|
||||
|
||||
|
|
@ -451,9 +454,6 @@ class ClearableFileInput(FileInput):
|
|||
return False
|
||||
return upload
|
||||
|
||||
def use_required_attribute(self, initial):
|
||||
return super().use_required_attribute(initial) and not initial
|
||||
|
||||
def value_omitted_from_data(self, data, files, name):
|
||||
return (
|
||||
super().value_omitted_from_data(data, files, name) and
|
||||
|
|
|
|||
|
|
@ -18,3 +18,9 @@ class FileInputTest(WidgetTest):
|
|||
def test_value_omitted_from_data(self):
|
||||
self.assertIs(self.widget.value_omitted_from_data({}, {}, 'field'), True)
|
||||
self.assertIs(self.widget.value_omitted_from_data({}, {'field': 'value'}, 'field'), False)
|
||||
|
||||
def test_use_required_attribute(self):
|
||||
# False when initial data exists. The file input is left blank by the
|
||||
# user to keep the existing, initial value.
|
||||
self.assertIs(self.widget.use_required_attribute(None), True)
|
||||
self.assertIs(self.widget.use_required_attribute('resume.txt'), False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue