Ticket #13564: 13564_form_get_field_css_classes.diff
File 13564_form_get_field_css_classes.diff, 5.6 KB (added by , 14 years ago) |
---|
-
django/forms/forms.py
135 135 """ 136 136 return u'initial-%s' % self.add_prefix(field_name) 137 137 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 138 151 def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): 139 152 "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 140 153 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. … … 497 510 extra_classes.add(self.form.error_css_class) 498 511 if self.field.required and hasattr(self.form, 'required_css_class'): 499 512 extra_classes.add(self.form.required_css_class) 513 extra_classes = self.form.get_field_css_classes(self, extra_classes) 500 514 return ' '.join(extra_classes) 501 515 502 516 def _is_hidden(self): -
tests/regressiontests/forms/forms.py
1885 1885 <tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr> 1886 1886 <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> 1887 1887 1888 # Check setting custom field css classes by overriding Form.get_field_css_classes function 1888 1889 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 1889 1905 1906 >>> p = Person({"name":"Joe"}) 1907 >>> p.is_valid() 1908 False 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 1890 1914 # Checking that the label for SplitDateTimeField is not being displayed ##### 1891 1915 1892 1916 >>> class EventForm(Form): -
docs/ref/forms/api.txt
386 386 errors. For example, you might want to present required form rows in bold and 387 387 highlight errors in red. 388 388 389 ``get_field_css_classes()``, ``error_css_class`` and ``required_css_class`` 390 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 391 392 .. method:: Form.get_field_css_classes 393 389 394 The :class:`Form` class has a couple of hooks you can use to add ``class`` 390 395 attributes to required rows or to rows with errors: simple set the 391 396 :attr:`Form.error_css_class` and/or :attr:`Form.required_css_class` 392 attributes ::397 attributes and/or override ``get_field_css_classes()`` method :: 393 398 394 399 class ContactForm(Form): 395 400 error_css_class = 'error' 396 401 required_css_class = 'required' 397 402 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 398 411 # ... and the rest of your fields here 399 412 400 Once you've done that, rows will be given ``"error"`` and/or ``"required"``401 classes, as needed. The HTML will look something like::413 Once you've done that, rows will be given various classes as defined by 414 mentioned attributes and method. The HTML will look something like:: 402 415 403 416 >>> f = ContactForm(data) 404 417 >>> 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> ... 409 422 410 423 .. _ref-forms-api-configuring-label: 411 424