Fixed #20630 -- Removed `maxlength` attribute from `NumberInput`.
This attribute is only allowed on inputs of type "text", "search", "url", "tel", "email", or "password". Thanks to yoyoma for the report and @bmispelon for the review.
This commit is contained in:
parent
6ef199a08e
commit
04628e2016
|
@ -370,13 +370,7 @@ class DecimalField(IntegerField):
|
||||||
|
|
||||||
def widget_attrs(self, widget):
|
def widget_attrs(self, widget):
|
||||||
attrs = super(DecimalField, self).widget_attrs(widget)
|
attrs = super(DecimalField, self).widget_attrs(widget)
|
||||||
if isinstance(widget, NumberInput):
|
if isinstance(widget, NumberInput) and self.decimal_places:
|
||||||
if self.max_digits is not None:
|
|
||||||
max_length = self.max_digits + 1 # for the sign
|
|
||||||
if self.decimal_places is None or self.decimal_places > 0:
|
|
||||||
max_length += 1 # for the dot
|
|
||||||
attrs['maxlength'] = max_length
|
|
||||||
if self.decimal_places:
|
|
||||||
attrs['step'] = '0.%s1' % ('0' * (self.decimal_places - 1))
|
attrs['step'] = '0.%s1' % ('0' * (self.decimal_places - 1))
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
|
@ -296,7 +296,7 @@ class FieldsTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_decimalfield_1(self):
|
def test_decimalfield_1(self):
|
||||||
f = DecimalField(max_digits=4, decimal_places=2)
|
f = DecimalField(max_digits=4, decimal_places=2)
|
||||||
self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" maxlength="6" />')
|
self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" />')
|
||||||
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '')
|
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '')
|
||||||
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None)
|
self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None)
|
||||||
self.assertEqual(f.clean('1'), Decimal("1"))
|
self.assertEqual(f.clean('1'), Decimal("1"))
|
||||||
|
@ -342,7 +342,7 @@ class FieldsTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_decimalfield_3(self):
|
def test_decimalfield_3(self):
|
||||||
f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5'))
|
f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5'))
|
||||||
self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.5" max="1.5" maxlength="6" type="number" id="id_f" />')
|
self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.5" max="1.5" type="number" id="id_f" />')
|
||||||
self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6')
|
self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6')
|
||||||
self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4')
|
self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4')
|
||||||
self.assertEqual(f.clean('1.5'), Decimal("1.5"))
|
self.assertEqual(f.clean('1.5'), Decimal("1.5"))
|
||||||
|
|
|
@ -559,7 +559,7 @@ class ModelFormsetTest(TestCase):
|
||||||
formset = AuthorBooksFormSet2(instance=author)
|
formset = AuthorBooksFormSet2(instance=author)
|
||||||
self.assertEqual(len(formset.forms), 1)
|
self.assertEqual(len(formset.forms), 1)
|
||||||
self.assertHTMLEqual(formset.forms[0].as_p(),
|
self.assertHTMLEqual(formset.forms[0].as_p(),
|
||||||
'<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input id="id_bookwithcustompk_set-0-my_pk" type="number" name="bookwithcustompk_set-0-my_pk" maxlength="6" /></p>\n'
|
'<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input id="id_bookwithcustompk_set-0-my_pk" type="number" name="bookwithcustompk_set-0-my_pk" /></p>\n'
|
||||||
'<p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p>')
|
'<p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p>')
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
|
|
Loading…
Reference in New Issue