Fixed #12466 - Set HTML class attributes for each field separately. Thanks for the patch, Bernd Schlapsi.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12153 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-09 22:51:03 +00:00
parent 4700200383
commit 5e83b79d26
2 changed files with 14 additions and 1 deletions

View File

@ -138,9 +138,9 @@ class BaseForm(StrAndUnicode):
"Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
top_errors = self.non_field_errors() # Errors that should be displayed above all fields. top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
output, hidden_fields = [], [] output, hidden_fields = [], []
html_class_attr = ''
for name, field in self.fields.items(): for name, field in self.fields.items():
html_class_attr = ''
bf = BoundField(self, field, name) bf = BoundField(self, field, name)
bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable. bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
if bf.is_hidden: if bf.is_hidden:

View File

@ -1816,6 +1816,12 @@ True
# The error_html_class and required_html_class attributes #################### # The error_html_class and required_html_class attributes ####################
>>> class Person(Form):
... name = CharField()
... is_cool = NullBooleanField()
... email = EmailField(required=False)
... age = IntegerField()
>>> p = Person({}) >>> p = Person({})
>>> p.error_css_class = 'error' >>> p.error_css_class = 'error'
>>> p.required_css_class = 'required' >>> p.required_css_class = 'required'
@ -1827,6 +1833,8 @@ True
<option value="2">Yes</option> <option value="2">Yes</option>
<option value="3">No</option> <option value="3">No</option>
</select></li> </select></li>
<li><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></li>
<li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></li>
>>> print p.as_p() >>> print p.as_p()
<ul class="errorlist"><li>This field is required.</li></ul> <ul class="errorlist"><li>This field is required.</li></ul>
@ -1836,6 +1844,9 @@ True
<option value="2">Yes</option> <option value="2">Yes</option>
<option value="3">No</option> <option value="3">No</option>
</select></p> </select></p>
<p><label for="id_email">Email:</label> <input type="text" name="email" id="id_email" /></p>
<ul class="errorlist"><li>This field is required.</li></ul>
<p class="required error"><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>
>>> print p.as_table() >>> print p.as_table()
<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> <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>
@ -1844,6 +1855,8 @@ True
<option value="2">Yes</option> <option value="2">Yes</option>
<option value="3">No</option> <option value="3">No</option>
</select></td></tr> </select></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
<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>