Opened 5 years ago

Closed 5 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 thenewguy)

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 thenewguy, 5 years ago

Description: modified (diff)

comment:2 by thenewguy, 5 years ago

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 from ClearableFileInput to FileInput this passes. This seems like more appropriate behavior to me

comment:3 by Mariusz Felisiak, 5 years ago

Cc: Jon Dufresne added
Component: UncategorizedForms
Summary: FileInput widget and use_required_attribute()FileInput shouldn't display required attribute when initial data exists.
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization
Version: 3.0master

comment:4 by shubham singh , 5 years ago

Cc: shubham singh added
Owner: changed from nobody to shubham singh
Status: newassigned

comment:5 by shubham singh , 5 years ago

Has patch: set

comment:6 by Mariusz Felisiak, 5 years ago

Needs documentation: set
Needs tests: set

comment:7 by Mariusz Felisiak, 5 years ago

Needs documentation: unset
Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In ffcf1a8e:

Fixed #31118 -- Made FileInput to avoid the required attribute when initial data exists.

Note: See TracTickets for help on using tickets.
Back to Top