Ticket #16304: 16304_v2.patch
File 16304_v2.patch, 3.2 KB (added by , 13 years ago) |
---|
-
docs/ref/forms/fields.txt
330 330 If provided, these arguments ensure that the string is at most or at least 331 331 the given length. 332 332 333 .. note:: 334 335 If you are using HTML5 templates, you can pass a placeholder attribute 336 containg a string, which will act as the placeholder for the resulting 337 input box. 338 333 339 ``ChoiceField`` 334 340 ~~~~~~~~~~~~~~~ 335 341 -
django/forms/fields.py
182 182 return result 183 183 184 184 class CharField(Field): 185 def __init__(self, max_length=None, min_length=None, *args, **kwargs): 186 self.max_length, self.min_length = max_length, min_length 185 def __init__(self, max_length=None, min_length=None, placeholder=None, 186 *args, **kwargs): 187 self.max_length, self.min_length, self.placeholder = (max_length, 188 min_length, placeholder) 187 189 super(CharField, self).__init__(*args, **kwargs) 188 190 if min_length is not None: 189 191 self.validators.append(validators.MinLengthValidator(min_length)) … … 201 203 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): 202 204 # The HTML attribute is maxlength, not max_length. 203 205 attrs.update({'maxlength': str(self.max_length)}) 206 if self.placeholder and isinstance(widget, TextInput): 207 attrs.update({'placeholder': self.placeholder}) 204 208 return attrs 205 209 206 210 class IntegerField(Field): -
tests/regressiontests/forms/tests/forms.py
930 930 password = CharField(max_length=10, widget=PasswordInput) 931 931 realname = CharField(max_length=10, widget=TextInput) # redundantly define widget, just to test 932 932 address = CharField() # no max_length defined here 933 data1 = CharField(placeholder='Enter random data') # checking for the HTML5 placeholder attribute 934 data2 = CharField(max_length=10, placeholder='Random')# this time with max_length given 933 935 934 936 p = UserRegistration(auto_id=False) 935 937 self.assertHTMLEqual(p.as_ul(), """<li>Username: <input type="text" name="username" maxlength="10" /></li> 936 938 <li>Password: <input type="password" name="password" maxlength="10" /></li> 937 939 <li>Realname: <input type="text" name="realname" maxlength="10" /></li> 938 <li>Address: <input type="text" name="address" /></li>""") 940 <li>Address: <input type="text" name="address" /></li> 941 <li>Data1: <input type="text" name="data1" placeholder="Enter random data" /></li> 942 <li>Data2: <input type="text" name="data2" maxlength="10" placeholder="Random" /></li>""") 939 943 940 944 # If you specify a custom "attrs" that includes the "maxlength" attribute, 941 945 # the Field's max_length attribute will override whatever "maxlength" you specify