Ticket #16304: 16304_v2.patch

File 16304_v2.patch, 3.2 KB (added by j4nu5, 3 years ago)

Adds placeholder attribute functionality to forms. Has docs. Has tests.

  • docs/ref/forms/fields.txt

     
    330330    If provided, these arguments ensure that the string is at most or at least
    331331    the given length.
    332332
     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
    333339``ChoiceField``
    334340~~~~~~~~~~~~~~~
    335341
  • django/forms/fields.py

     
    182182        return result
    183183
    184184class 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)
    187189        super(CharField, self).__init__(*args, **kwargs)
    188190        if min_length is not None:
    189191            self.validators.append(validators.MinLengthValidator(min_length))
     
    201203        if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
    202204            # The HTML attribute is maxlength, not max_length.
    203205            attrs.update({'maxlength': str(self.max_length)})
     206        if self.placeholder and isinstance(widget, TextInput):
     207            attrs.update({'placeholder': self.placeholder})
    204208        return attrs
    205209
    206210class IntegerField(Field):
  • tests/regressiontests/forms/tests/forms.py

     
    930930            password = CharField(max_length=10, widget=PasswordInput)
    931931            realname = CharField(max_length=10, widget=TextInput) # redundantly define widget, just to test
    932932            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
    933935
    934936        p = UserRegistration(auto_id=False)
    935937        self.assertHTMLEqual(p.as_ul(), """<li>Username: <input type="text" name="username" maxlength="10" /></li>
    936938<li>Password: <input type="password" name="password" maxlength="10" /></li>
    937939<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>""")
    939943
    940944        # If you specify a custom "attrs" that includes the "maxlength" attribute,
    941945        # the Field's max_length attribute will override whatever "maxlength" you specify
Back to Top