Ticket #16304: placeholder.patch

File placeholder.patch, 1.3 KB (added by avenet, 4 years ago)

Patch for adding placeholder attribute on CharField class

  • fields.py

     
    179179        return result
    180180
    181181class CharField(Field):
    182     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    183         self.max_length, self.min_length = max_length, min_length
     182    def __init__(self, max_length=None, min_length=None, placeholder=None, *args, **kwargs):
     183        self.max_length, self.min_length, self.placeholder = max_length, min_length, placeholder
    184184        super(CharField, self).__init__(*args, **kwargs)
    185185        if min_length is not None:
    186186            self.validators.append(validators.MinLengthValidator(min_length))
     
    194194        return smart_unicode(value)
    195195
    196196    def widget_attrs(self, widget):
     197        attrs = {}
     198        if self.placeholder is not None  and isinstance(widget, TextInput):
     199            attrs["placeholder"] = str(self.placeholder)
    197200        if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
    198201            # The HTML attribute is maxlength, not max_length.
    199             return {'maxlength': str(self.max_length)}
     202            attrs['maxlength'] = str(self.max_length)
     203        if attrs:
     204            return attrs
    200205
    201206class IntegerField(Field):
    202207    default_error_messages = {
Back to Top