Ticket #13564: 13564_form_get_field_css_classes.diff

File 13564_form_get_field_css_classes.diff, 5.6 KB (added by Robert Lujo, 14 years ago)

New method Form.get_css_classes - with tests and docs

  • django/forms/forms.py

     
    135135        """
    136136        return u'initial-%s' % self.add_prefix(field_name)
    137137
     138    def get_field_css_classes(self, bf, css_classes):
     139        """
     140        Returns the bound field's css classes as set() of strings. Called for
     141        each bounded field (parameter *bf*). Parameter *css_classes* holds
     142        current field's set of css classes.
     143
     144        Subclasses may wish to override.
     145
     146        See also form class attributes *error_css_class* and
     147        *required_css_class*.
     148        """
     149        return css_classes
     150
    138151    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
    139152        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    140153        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
     
    497510            extra_classes.add(self.form.error_css_class)
    498511        if self.field.required and hasattr(self.form, 'required_css_class'):
    499512            extra_classes.add(self.form.required_css_class)
     513        extra_classes = self.form.get_field_css_classes(self, extra_classes)
    500514        return ' '.join(extra_classes)
    501515
    502516    def _is_hidden(self):
  • tests/regressiontests/forms/forms.py

     
    18851885<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
    18861886<tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="age" id="id_age" /></td></tr>
    18871887
     1888# Check setting custom field css classes by overriding Form.get_field_css_classes function
    18881889
     1890>>> class Person(Form):
     1891...     error_css_class = 'error'
     1892...     required_css_class = 'required'
     1893...
     1894...     name = CharField()
     1895...     is_cool = BooleanField()
     1896...     age = IntegerField(required=False)
     1897...
     1898...     def get_field_css_classes(self, bf, css_classes):
     1899...         if "required" not in css_classes:
     1900...             css_classes.add("not_required")
     1901...         if "error" not in css_classes:
     1902...             css_classes.add("not_error")
     1903...         css_classes.add("django-%s" % bf.field.__class__.__name__.lower())
     1904...         return css_classes
    18891905
     1906>>> p = Person({"name":"Joe"})
     1907>>> p.is_valid()
     1908False
     1909>>> print p.as_ul()
     1910<li class="required not_error django-charfield"><label for="id_name">Name:</label> <input type="text" name="name" value="Joe" id="id_name" /></li>
     1911<li class="django-booleanfield required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_is_cool">Is cool:</label> <input type="checkbox" name="is_cool" id="id_is_cool" /></li>
     1912<li class="django-integerfield not_required not_error"><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></li>
     1913
    18901914# Checking that the label for SplitDateTimeField is not being displayed #####
    18911915
    18921916>>> class EventForm(Form):
  • docs/ref/forms/api.txt

     
    386386errors. For example, you might want to present required form rows in bold and
    387387highlight errors in red.
    388388
     389``get_field_css_classes()``, ``error_css_class`` and ``required_css_class``
     390~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     391
     392.. method:: Form.get_field_css_classes
     393
    389394The :class:`Form` class has a couple of hooks you can use to add ``class``
    390395attributes to required rows or to rows with errors: simple set the
    391396:attr:`Form.error_css_class` and/or :attr:`Form.required_css_class`
    392 attributes::
     397attributes and/or override ``get_field_css_classes()`` method ::
    393398
    394399    class ContactForm(Form):
    395400        error_css_class = 'error'
    396401        required_css_class = 'required'
    397402
     403        def get_field_css_classes(self, bf, css_classes):
     404            if "required" not in css_classes:
     405                css_classes.add("not_required")
     406            if "error" not in css_classes:
     407                css_classes.add("not_error")
     408            css_classes.add("django-%s" % bf.field.__class__.__name__.lower())
     409            return css_classes
     410
    398411        # ... and the rest of your fields here
    399412
    400 Once you've done that, rows will be given ``"error"`` and/or ``"required"``
    401 classes, as needed. The HTML will look something like::
     413Once you've done that, rows will be given various classes as defined by
     414mentioned attributes and method. The HTML will look something like::
    402415
    403416    >>> f = ContactForm(data)
    404417    >>> print f.as_table()
    405     <tr class="required"><th><label for="id_subject">Subject:</label>    ...
    406     <tr class="required"><th><label for="id_message">Message:</label>    ...
    407     <tr class="required error"><th><label for="id_sender">Sender:</label>      ...
    408     <tr><th><label for="id_cc_myself">Cc myself:<label> ...
     418    <tr class="django-charfield required not_error"><th><label for="id_subject">Subject:</label>    ...
     419    <tr class="django-charfield required not_error"><th><label for="id_message">Message:</label>    ...
     420    <tr class="django-emailfield required error"><th><label for="id_sender">Sender:</label>      ...
     421    <tr class="django-booleanfield not_required not_error"><th><label for="id_cc_myself">Cc myself:</label> ...
    409422
    410423.. _ref-forms-api-configuring-label:
    411424
Back to Top