Ticket #15912: 15912.diff

File 15912.diff, 2.0 KB (added by mmcnickle, 13 years ago)

Make sure CharField? always returns a dictionary from widget_attrs()

  • tests/regressiontests/forms/tests/fields.py

     
    3636from django.forms.widgets import RadioFieldRenderer
    3737from django.utils.unittest import TestCase
    3838
    39 
    4039def fix_os_paths(x):
    4140    if isinstance(x, basestring):
    4241        return x.replace('\\', '/')
     
    109108        self.assertEqual(f.max_length, None)
    110109        self.assertEqual(f.min_length, 10)
    111110
     111    def test_charfield_widget_attrs(self):
     112        f = CharField()
     113        # Return an empty dictionary if min_length is None
     114        self.assertEqual(f.widget_attrs(TextInput()), {})
     115
     116        # Or if the widget is not TextInput or PasswordInput
     117        f = CharField(max_length=10)
     118        self.assertEqual(f.widget_attrs(HiddenInput()), {})
     119
     120        # Otherwise, should return a maxlength attribute equal to max_length
     121        self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '10'})
     122        self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '10'})
     123
     124        f = CharField(max_length=20)
     125        self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '20'})
     126        self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '20'})
     127
    112128    # IntegerField ################################################################
    113129
    114130    def test_integerfield_1(self):
  • django/forms/fields.py

     
    199199        if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
    200200            # The HTML attribute is maxlength, not max_length.
    201201            return {'maxlength': str(self.max_length)}
     202        else:
     203            return {}
    202204
    203205class IntegerField(Field):
    204206    default_error_messages = {
Back to Top