From 5b4e4c63594bdac5c589cff90c3f88c99b62305e Mon Sep 17 00:00:00 2001
From: Simon Charette <charette.s@gmail.com>
Date: Wed, 19 Jun 2013 23:42:23 -0400
Subject: [PATCH] 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.
---
django/forms/fields.py | 10 ++--------
tests/forms_tests/tests/test_fields.py | 4 ++--
tests/model_formsets/tests.py | 2 +-
3 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 52bcf94..c4bc3fa8 100644
a
|
b
|
class DecimalField(IntegerField):
|
370 | 370 | |
371 | 371 | def widget_attrs(self, widget): |
372 | 372 | attrs = super(DecimalField, self).widget_attrs(widget) |
373 | | if isinstance(widget, NumberInput): |
374 | | if self.max_digits is not None: |
375 | | max_length = self.max_digits + 1 # for the sign |
376 | | if self.decimal_places is None or self.decimal_places > 0: |
377 | | max_length += 1 # for the dot |
378 | | attrs['maxlength'] = max_length |
379 | | if self.decimal_places: |
380 | | attrs['step'] = '0.%s1' % ('0' * (self.decimal_places-1)) |
| 373 | if isinstance(widget, NumberInput) and self.decimal_places: |
| 374 | attrs['step'] = '0.%s1' % ('0' * (self.decimal_places - 1)) |
381 | 375 | return attrs |
382 | 376 | |
383 | 377 | |
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
index 47c637b..f7d8350 100644
a
|
b
|
class FieldsTests(SimpleTestCase):
|
296 | 296 | |
297 | 297 | def test_decimalfield_1(self): |
298 | 298 | f = DecimalField(max_digits=4, decimal_places=2) |
299 | | self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" maxlength="6" />') |
| 299 | self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" />') |
300 | 300 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') |
301 | 301 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
302 | 302 | self.assertEqual(f.clean('1'), Decimal("1")) |
… |
… |
class FieldsTests(SimpleTestCase):
|
342 | 342 | |
343 | 343 | def test_decimalfield_3(self): |
344 | 344 | f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) |
345 | | self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.5" max="1.5" maxlength="6" type="number" id="id_f" />') |
| 345 | self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.5" max="1.5" type="number" id="id_f" />') |
346 | 346 | self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6') |
347 | 347 | self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4') |
348 | 348 | self.assertEqual(f.clean('1.5'), Decimal("1.5")) |
diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
index 03cd3b0..62870c7 100644
a
|
b
|
class ModelFormsetTest(TestCase):
|
559 | 559 | formset = AuthorBooksFormSet2(instance=author) |
560 | 560 | self.assertEqual(len(formset.forms), 1) |
561 | 561 | self.assertHTMLEqual(formset.forms[0].as_p(), |
562 | | '<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' |
| 562 | '<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' |
563 | 563 | '<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>') |
564 | 564 | |
565 | 565 | data = { |