Ticket #3512: 3512.diff

File 3512.diff, 6.7 KB (added by Chris Beaven, 16 years ago)
  • django/forms/forms.py

     
    33"""
    44
    55from copy import deepcopy
     6try:
     7    set
     8except NameError:
     9    from sets import Set as set   # Python 2.3 fallback
    610
    711from django.utils.datastructures import SortedDict
    812from django.utils.html import escape
     
    6872    # class is different than Form. See the comments by the Form class for more
    6973    # information. Any improvements to the form API should be made to *this*
    7074    # class, not to the Form class.
     75    error_html_class = None
     76    required_html_class = None
     77
    7178    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    7279                 initial=None, error_class=ErrorList, label_suffix=':',
    7380                 empty_permitted=False):
     
    146153                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
    147154                hidden_fields.append(unicode(bf))
    148155            else:
     156                html_class = bf.html_class()
     157                if html_class:
     158                    html_class = ' class="%s"' % html_class
    149159                if errors_on_separate_row and bf_errors:
    150160                    output.append(error_row % force_unicode(bf_errors))
    151161                if bf.label:
     
    162172                    help_text = help_text_html % force_unicode(field.help_text)
    163173                else:
    164174                    help_text = u''
    165                 output.append(normal_row % {'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text})
     175                output.append(normal_row % {'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text, 'html_class': html_class})
    166176        if top_errors:
    167177            output.insert(0, error_row % force_unicode(top_errors))
    168178        if hidden_fields: # Insert any hidden fields in the last row.
     
    176186                    # that users write): if there are only top errors, we may
    177187                    # not be able to conscript the last row for our purposes,
    178188                    # so insert a new, empty row.
    179                     last_row = normal_row % {'errors': '', 'label': '', 'field': '', 'help_text': ''}
     189                    last_row = normal_row % {'errors': '', 'label': '', 'field': '', 'help_text': '', 'html_class': ''}
    180190                    output.append(last_row)
    181191                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
    182192            else:
     
    187197
    188198    def as_table(self):
    189199        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    190         return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
     200        return self._html_output(u'<tr%(html_class)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
    191201
    192202    def as_ul(self):
    193203        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    194         return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
     204        return self._html_output(u'<li%(html_class)s>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
    195205
    196206    def as_p(self):
    197207        "Returns this form rendered as HTML <p>s."
    198         return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'%s', '</p>', u' %s', True)
     208        return self._html_output(u'<p%(html_class)s>%(label)s %(field)s%(help_text)s</p>', u'%s', '</p>', u' %s', True)
    199209
    200210    def non_field_errors(self):
    201211        """
     
    363373        else:
    364374            name = self.html_initial_name
    365375        return widget.render(name, data, attrs=attrs)
    366        
     376
    367377    def as_text(self, attrs=None, **kwargs):
    368378        """
    369379        Returns a string of HTML for representing this as an <input type="text">.
     
    403413            contents = u'<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, unicode(contents))
    404414        return mark_safe(contents)
    405415
     416    def html_class(self, extra_classes=None):
     417        "Returns a string of space-separated HTML classes for this field."
     418        if not extra_classes:
     419            class_list = set()
     420        else:
     421            if isinstance(extra_classes, basestring):
     422                extra_classes = extra_classes.split()
     423            class_list = set(extra_classes)
     424        if self.form.error_html_class and self.errors:
     425            class_list.add(self.form.error_html_class)
     426        if self.form.required_html_class and self.field.required:
     427            class_list.add(self.form.required_html_class)
     428        return ' '.join(class_list)
     429
    406430    def _is_hidden(self):
    407431        "Returns True if this BoundField's widget is hidden."
    408432        return self.field.widget.is_hidden
  • tests/regressiontests/forms/forms.py

     
    17491749>>> form.is_valid()
    17501750True
    17511751
     1752
     1753# The error_html_class and required_html_class attributes ####################
     1754
     1755>>> p = Person({})
     1756>>> p.error_html_class = 'error'
     1757>>> p.required_html_class = 'required'
     1758
     1759>>> print p.as_ul()
     1760<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
     1761<li class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
     1762<option value="1" selected="selected">Unknown</option>
     1763<option value="2">Yes</option>
     1764<option value="3">No</option>
     1765</select></li>
     1766
     1767>>> print p.as_p()
     1768<ul class="errorlist"><li>This field is required.</li></ul>
     1769<p class="required error"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
     1770<p class="required"><label for="id_is_cool">Is cool:</label> <select name="is_cool" id="id_is_cool">
     1771<option value="1" selected="selected">Unknown</option>
     1772<option value="2">Yes</option>
     1773<option value="3">No</option>
     1774</select></p>
     1775
     1776>>> print p.as_table()
     1777<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr>
     1778<tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool">
     1779<option value="1" selected="selected">Unknown</option>
     1780<option value="2">Yes</option>
     1781<option value="3">No</option>
     1782</select></td></tr>
    17521783"""
Back to Top