Ticket #3512: 3512.diff
File 3512.diff, 6.7 KB (added by , 16 years ago) |
---|
-
django/forms/forms.py
3 3 """ 4 4 5 5 from copy import deepcopy 6 try: 7 set 8 except NameError: 9 from sets import Set as set # Python 2.3 fallback 6 10 7 11 from django.utils.datastructures import SortedDict 8 12 from django.utils.html import escape … … 68 72 # class is different than Form. See the comments by the Form class for more 69 73 # information. Any improvements to the form API should be made to *this* 70 74 # class, not to the Form class. 75 error_html_class = None 76 required_html_class = None 77 71 78 def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, 72 79 initial=None, error_class=ErrorList, label_suffix=':', 73 80 empty_permitted=False): … … 146 153 top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors]) 147 154 hidden_fields.append(unicode(bf)) 148 155 else: 156 html_class = bf.html_class() 157 if html_class: 158 html_class = ' class="%s"' % html_class 149 159 if errors_on_separate_row and bf_errors: 150 160 output.append(error_row % force_unicode(bf_errors)) 151 161 if bf.label: … … 162 172 help_text = help_text_html % force_unicode(field.help_text) 163 173 else: 164 174 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}) 166 176 if top_errors: 167 177 output.insert(0, error_row % force_unicode(top_errors)) 168 178 if hidden_fields: # Insert any hidden fields in the last row. … … 176 186 # that users write): if there are only top errors, we may 177 187 # not be able to conscript the last row for our purposes, 178 188 # 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': ''} 180 190 output.append(last_row) 181 191 output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender 182 192 else: … … 187 197 188 198 def as_table(self): 189 199 "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) 191 201 192 202 def as_ul(self): 193 203 "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) 195 205 196 206 def as_p(self): 197 207 "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) 199 209 200 210 def non_field_errors(self): 201 211 """ … … 363 373 else: 364 374 name = self.html_initial_name 365 375 return widget.render(name, data, attrs=attrs) 366 376 367 377 def as_text(self, attrs=None, **kwargs): 368 378 """ 369 379 Returns a string of HTML for representing this as an <input type="text">. … … 403 413 contents = u'<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, unicode(contents)) 404 414 return mark_safe(contents) 405 415 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 406 430 def _is_hidden(self): 407 431 "Returns True if this BoundField's widget is hidden." 408 432 return self.field.widget.is_hidden -
tests/regressiontests/forms/forms.py
1749 1749 >>> form.is_valid() 1750 1750 True 1751 1751 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> 1752 1783 """