Opened 6 years ago
Closed 6 years ago
#31118 closed Cleanup/optimization (fixed)
FileInput shouldn't display required attribute when initial data exists.
| Reported by: | thenewguy | Owned by: | shubham singh |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Jon Dufresne, shubham singh | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I think that ClearableFileInput.use_required_attribute() (https://github.com/django/django/blob/e703b93a656b78b9b444bb3a9980e305ed002a70/django/forms/widgets.py#L454) should be moved to FileInput.use_required_attribute() so that required is not output on the html input element that represents FileInput when a file is already set (e.g. already saved on a model instance that is being edited).
Maybe I am overlooking a use case where this is not desirable? I can not think of one.
Change History (8)
comment:1 by , 6 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 6 years ago
comment:3 by , 6 years ago
| Cc: | added |
|---|---|
| Component: | Uncategorized → Forms |
| Summary: | FileInput widget and use_required_attribute() → FileInput shouldn't display required attribute when initial data exists. |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Cleanup/optimization |
| Version: | 3.0 → master |
comment:4 by , 6 years ago
| Cc: | added |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:6 by , 6 years ago
| Needs documentation: | set |
|---|---|
| Needs tests: | set |
comment:7 by , 6 years ago
| Needs documentation: | unset |
|---|---|
| Needs tests: | unset |
| Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
This might explain better:
from django import forms from django.core.files.base import ContentFile from django.test import SimpleTestCase class FileForm(forms.Form): file = forms.FileField(widget=forms.FileInput) class FileInputRenderTest(SimpleTestCase): def test_file_input(self): form = FileForm() field = form['file'] self.assertEqual(str(field), '<input type="file" name="file" required id="id_file">') def test_file_input_with_initial_value(self): # fails because it outputs the 'required' attr form = FileForm(initial={'file': ContentFile(b'baz', name='baz.txt')}) field = form['file'] self.assertEqual(str(field), '<input type="file" name="file" id="id_file">')If the
use_required_attribute()method is copied fromClearableFileInputtoFileInputthis passes. This seems like more appropriate behavior to me