Fixed #27015 -- Prevented HTML-invalid minlength/maxlength on hidden inputs
This commit is contained in:
parent
6a8372e6ec
commit
3569ba0333
|
@ -236,10 +236,10 @@ class CharField(Field):
|
||||||
|
|
||||||
def widget_attrs(self, widget):
|
def widget_attrs(self, widget):
|
||||||
attrs = super(CharField, self).widget_attrs(widget)
|
attrs = super(CharField, self).widget_attrs(widget)
|
||||||
if self.max_length is not None:
|
if self.max_length is not None and not widget.is_hidden:
|
||||||
# The HTML attribute is maxlength, not max_length.
|
# The HTML attribute is maxlength, not max_length.
|
||||||
attrs['maxlength'] = str(self.max_length)
|
attrs['maxlength'] = str(self.max_length)
|
||||||
if self.min_length is not None:
|
if self.min_length is not None and not widget.is_hidden:
|
||||||
# The HTML attribute is minlength, not min_length.
|
# The HTML attribute is minlength, not min_length.
|
||||||
attrs['minlength'] = str(self.min_length)
|
attrs['minlength'] = str(self.min_length)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.forms import (
|
from django.forms import (
|
||||||
CharField, PasswordInput, Textarea, TextInput, ValidationError,
|
CharField, HiddenInput, PasswordInput, Textarea, TextInput, ValidationError,
|
||||||
)
|
)
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
@ -79,7 +79,9 @@ class CharFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
||||||
|
|
||||||
def test_charfield_widget_attrs(self):
|
def test_charfield_widget_attrs(self):
|
||||||
"""
|
"""
|
||||||
CharField.widget_attrs() always returns a dictionary (#15912).
|
CharField.widget_attrs() always returns a dictionary and includes
|
||||||
|
minlength/maxlength if min_length/max_length are defined on the field
|
||||||
|
and the widget is not hidden.
|
||||||
"""
|
"""
|
||||||
# Return an empty dictionary if max_length and min_length are both None.
|
# Return an empty dictionary if max_length and min_length are both None.
|
||||||
f = CharField()
|
f = CharField()
|
||||||
|
@ -104,6 +106,7 @@ class CharFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
||||||
self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '10', 'minlength': '5'})
|
self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '10', 'minlength': '5'})
|
||||||
self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '10', 'minlength': '5'})
|
self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '10', 'minlength': '5'})
|
||||||
self.assertEqual(f.widget_attrs(Textarea()), {'maxlength': '10', 'minlength': '5'})
|
self.assertEqual(f.widget_attrs(Textarea()), {'maxlength': '10', 'minlength': '5'})
|
||||||
|
self.assertEqual(f.widget_attrs(HiddenInput()), {})
|
||||||
|
|
||||||
def test_charfield_strip(self):
|
def test_charfield_strip(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue