Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15912 -- Ensured that `forms.CharField.widget_attrs()` always …

…returns a dictionary. Thanks to tsabi and rubyruy for the report and to mmcnickle and prestontimmons for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17096 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a6ccc8cc06e6b304e35f7a16f6067e6fa09b63ce 1 parent fae75a3
Julien Phalip authored November 15, 2011
4  django/forms/fields.py
@@ -197,9 +197,11 @@ def to_python(self, value):
197 197
         return smart_unicode(value)
198 198
 
199 199
     def widget_attrs(self, widget):
  200
+        attrs = super(CharField, self).widget_attrs(widget)
200 201
         if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
201 202
             # The HTML attribute is maxlength, not max_length.
202  
-            return {'maxlength': str(self.max_length)}
  203
+            attrs.update({'maxlength': str(self.max_length)})
  204
+        return attrs
203 205
 
204 206
 class IntegerField(Field):
205 207
     default_error_messages = {
17  tests/regressiontests/forms/tests/fields.py
@@ -136,6 +136,23 @@ def test_charfield_5(self):
136 136
         self.assertEqual(f.max_length, None)
137 137
         self.assertEqual(f.min_length, 10)
138 138
 
  139
+    def test_charfield_widget_attrs(self):
  140
+        """
  141
+        Ensure that CharField.widget_attrs() always returns a dictionary.
  142
+        Refs #15912
  143
+        """
  144
+        # Return an empty dictionary if max_length is None
  145
+        f = CharField()
  146
+        self.assertEqual(f.widget_attrs(TextInput()), {})
  147
+
  148
+        # Or if the widget is not TextInput or PasswordInput
  149
+        f = CharField(max_length=10)
  150
+        self.assertEqual(f.widget_attrs(HiddenInput()), {})
  151
+
  152
+        # Otherwise, return a maxlength attribute equal to max_length
  153
+        self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '10'})
  154
+        self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '10'})
  155
+
139 156
     # IntegerField ################################################################
140 157
 
141 158
     def test_integerfield_1(self):

0 notes on commit a6ccc8c

Please sign in to comment.
Something went wrong with that request. Please try again.