mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #27037 -- Prevented required attribute on ClearableFileInput when initial data exists.
This commit is contained in:
parent
f842d1011c
commit
fab46ce6f5
8 changed files with 66 additions and 18 deletions
|
@ -2360,6 +2360,15 @@ Password: <input type="password" name="password" required />
|
|||
'<tr><th>File1:</th><td><input type="file" name="file1" required /></td></tr>',
|
||||
)
|
||||
|
||||
# A required file field with initial data should not contain the
|
||||
# required HTML attribute. The file input is left blank by the user to
|
||||
# keep the existing, initial value.
|
||||
f = FileForm(initial={'file1': 'resume.txt'}, auto_id=False)
|
||||
self.assertHTMLEqual(
|
||||
f.as_table(),
|
||||
'<tr><th>File1:</th><td><input type="file" name="file1" /></td></tr>',
|
||||
)
|
||||
|
||||
def test_basic_processing_in_view(self):
|
||||
class UserRegistration(Form):
|
||||
username = CharField(max_length=10)
|
||||
|
|
|
@ -112,3 +112,11 @@ class CheckboxSelectMultipleTest(WidgetTest):
|
|||
</ul>
|
||||
"""
|
||||
self.check_html(widget, 'letters', ['a', 'c'], html=html)
|
||||
|
||||
def test_use_required_attribute(self):
|
||||
widget = self.widget(choices=self.beatles)
|
||||
# Always False because browser validation would require all checkboxes
|
||||
# to be checked instead of at least one.
|
||||
self.assertIs(widget.use_required_attribute(None), False)
|
||||
self.assertIs(widget.use_required_attribute([]), False)
|
||||
self.assertIs(widget.use_required_attribute(['J', 'P']), False)
|
||||
|
|
|
@ -143,3 +143,9 @@ class ClearableFileInputTest(WidgetTest):
|
|||
|
||||
html = self.widget.render('myfile', NoURLFieldFile())
|
||||
self.assertHTMLEqual(html, '<input name="myfile" type="file" />')
|
||||
|
||||
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)
|
||||
|
|
|
@ -8,3 +8,10 @@ class HiddenInputTest(WidgetTest):
|
|||
|
||||
def test_render(self):
|
||||
self.check_html(self.widget, 'email', '', html='<input type="hidden" name="email" />')
|
||||
|
||||
def test_use_required_attribute(self):
|
||||
# Always False to avoid browser validation on inputs hidden from the
|
||||
# user.
|
||||
self.assertIs(self.widget.use_required_attribute(None), False)
|
||||
self.assertIs(self.widget.use_required_attribute(''), False)
|
||||
self.assertIs(self.widget.use_required_attribute('foo'), False)
|
||||
|
|
|
@ -76,3 +76,9 @@ class TextInputTest(WidgetTest):
|
|||
def test_attrs_safestring(self):
|
||||
widget = TextInput(attrs={'onBlur': mark_safe("function('foo')")})
|
||||
self.check_html(widget, 'email', '', html='<input onBlur="function(\'foo\')" type="text" name="email" />')
|
||||
|
||||
def test_use_required_attribute(self):
|
||||
# Text inputs can safely trigger the browser validation.
|
||||
self.assertIs(self.widget.use_required_attribute(None), True)
|
||||
self.assertIs(self.widget.use_required_attribute(''), True)
|
||||
self.assertIs(self.widget.use_required_attribute('resume.txt'), True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue