Ticket #16304: 16304.patch

File 16304.patch, 3.9 KB (added by Kushagra Sinha, 13 years ago)

Adds placeholder attribute functionality to Model forms and normal forms. Has docs. Does not have tests

  • docs/ref/models/fields.txt

     
    377377
    378378.. note::
    379379
     380    If you are using HTML5 templates and wish to have a placeholder attribute
     381    in your input box, supply a placeholder attribute containing a string when
     382    creating your model fields. Using :doc:`Model Forms
     383    </topics/forms/modelforms>` will automatically take care of the
     384    placeholder attribute. Refer :doc:`Form Fields </ref/forms/fields>` when
     385    creating your own forms.
     386
     387.. note::
     388
    380389    If you are writing an application that must be portable to multiple
    381390    database backends, you should be aware that there are restrictions on
    382391    ``max_length`` for some backends. Refer to the :doc:`database backend
  • 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/db/models/fields/__init__.py

     
    7979            serialize=True, unique_for_date=None, unique_for_month=None,
    8080            unique_for_year=None, choices=None, help_text='', db_column=None,
    8181            db_tablespace=None, auto_created=False, validators=[],
    82             error_messages=None):
     82            error_messages=None, placeholder=None):
    8383        self.name = name
    8484        self.verbose_name = verbose_name
    8585        self.primary_key = primary_key
     
    102102        self.db_column = db_column
    103103        self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
    104104        self.auto_created = auto_created
     105        self.placeholder = placeholder
    105106
    106107        # Set db_index to True if the field has a relationship and doesn't
    107108        # explicitly set db_index.
     
    622623        # will be validated twice. This is considered acceptable since we want
    623624        # the value in the form field (to pass into widget for example).
    624625        defaults = {'max_length': self.max_length}
     626        if self.placeholder:
     627            defaults.update({'placeholder': self.placeholder})
    625628        defaults.update(kwargs)
    626629        return super(CharField, self).formfield(**defaults)
    627630
  • 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': str(self.placeholder)})
    204208        return attrs
    205209
    206210class IntegerField(Field):
Back to Top