Ticket #20630: 0001-Fixed-20630-Removed-maxlength-attribute-from-NumberI.patch

File 0001-Fixed-20630-Removed-maxlength-attribute-from-NumberI.patch, 4.1 KB (added by Simon Charette, 11 years ago)
  • django/forms/fields.py

    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):  
    370370
    371371    def widget_attrs(self, widget):
    372372        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))
    381375        return attrs
    382376
    383377
  • tests/forms_tests/tests/test_fields.py

    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):  
    296296
    297297    def test_decimalfield_1(self):
    298298        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" />')
    300300        self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '')
    301301        self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None)
    302302        self.assertEqual(f.clean('1'), Decimal("1"))
    class FieldsTests(SimpleTestCase):  
    342342
    343343    def test_decimalfield_3(self):
    344344        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" />')
    346346        self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6')
    347347        self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4')
    348348        self.assertEqual(f.clean('1.5'), Decimal("1.5"))
  • tests/model_formsets/tests.py

    diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
    index 03cd3b0..62870c7 100644
    a b class ModelFormsetTest(TestCase):  
    559559        formset = AuthorBooksFormSet2(instance=author)
    560560        self.assertEqual(len(formset.forms), 1)
    561561        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'
    563563            '<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>')
    564564
    565565        data = {
Back to Top