Ticket #13564: 13564_form_get_field_css_classes.diff

File 13564_form_get_field_css_classes.diff, 5.6 KB (added by Robert Lujo, 16 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
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     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
    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
    18881889
     1890
     1891
     1892
     1893
     1894
     1895
     1896
     1897
     1898
     1899
     1900
     1901
     1902
     1903
     1904
    18891905
     1906
     1907
     1908
     1909
     1910
     1911
     1912
     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
     390
     391
     392
     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::
    393398
    394399    class ContactForm(Form):
    395400        error_css_class = 'error'
    396401        required_css_class = 'required'
    397402
     403
     404
     405
     406
     407
     408
     409
     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
     414d. 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=""><th><label for="id_subject">Subject:</label>    ...
     419    <tr class=""><th><label for="id_message">Message:</label>    ...
     420    <tr class="required error"><th><label for="id_sender">Sender:</label>      ...
     421    <trlabel> ...
    409422
    410423.. _ref-forms-api-configuring-label:
    411424
Back to Top