newforms: Normalized all error <ul>s to use class='errorlist'

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4120 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-11-27 04:56:33 +00:00
parent 49236b95e9
commit 190c987e63
2 changed files with 24 additions and 23 deletions

View File

@ -76,11 +76,11 @@ class Form(object):
output = [] output = []
if self.errors.get(NON_FIELD_ERRORS): if self.errors.get(NON_FIELD_ERRORS):
# Errors not corresponding to a particular field are displayed at the top. # Errors not corresponding to a particular field are displayed at the top.
output.append(u'<tr><td colspan="2"><ul>%s</ul></td></tr>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors[NON_FIELD_ERRORS]])) output.append(u'<tr><td colspan="2">%s</td></tr>' % self.non_field_errors())
for name, field in self.fields.items(): for name, field in self.fields.items():
bf = BoundField(self, field, name) bf = BoundField(self, field, name)
if bf.errors: if bf.errors:
output.append(u'<tr><td colspan="2"><ul>%s</ul></td></tr>' % u'\n'.join([u'<li>%s</li>' % e for e in bf.errors])) output.append(u'<tr><td colspan="2">%s</td></tr>' % bf.errors)
output.append(u'<tr><td>%s:</td><td>%s</td></tr>' % (bf.label, bf)) output.append(u'<tr><td>%s:</td><td>%s</td></tr>' % (bf.label, bf))
return u'\n'.join(output) return u'\n'.join(output)
@ -89,22 +89,23 @@ class Form(object):
output = [] output = []
if self.errors.get(NON_FIELD_ERRORS): if self.errors.get(NON_FIELD_ERRORS):
# Errors not corresponding to a particular field are displayed at the top. # Errors not corresponding to a particular field are displayed at the top.
output.append(u'<li><ul>%s</ul></li>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors[NON_FIELD_ERRORS]])) output.append(u'<li>%s</li>' % self.non_field_errors())
for name, field in self.fields.items(): for name, field in self.fields.items():
bf = BoundField(self, field, name) bf = BoundField(self, field, name)
line = u'<li>' line = u'<li>'
if bf.errors: if bf.errors:
line += u'<ul>%s</ul>' % u'\n'.join([u'<li>%s</li>' % e for e in bf.errors]) line += str(bf.errors)
line += u'%s: %s</li>' % (bf.label, bf) line += u'%s: %s</li>' % (bf.label, bf)
output.append(line) output.append(line)
return u'\n'.join(output) return u'\n'.join(output)
def non_field_errors(self): def non_field_errors(self):
""" """
Returns a list of errors that aren't associated with a particular Returns an ErrorList of errors that aren't associated with a particular
field -- i.e., from Form.clean(). field -- i.e., from Form.clean(). Returns an empty ErrorList if there
are none.
""" """
return self.errors.get(NON_FIELD_ERRORS, []) return self.errors.get(NON_FIELD_ERRORS, ErrorList())
def full_clean(self): def full_clean(self):
""" """

View File

@ -1173,23 +1173,23 @@ Empty dictionaries are valid, too.
>>> p.is_valid() >>> p.is_valid()
False False
>>> print p >>> print p
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
>>> print p.as_table() >>> print p.as_table()
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> <tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> <tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> <tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr>
>>> print p.as_ul() >>> print p.as_ul()
<li><ul><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li> <li><ul class="errorlist"><li>This field is required.</li></ul>First name: <input type="text" name="first_name" /></li>
<li><ul><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li> <li><ul class="errorlist"><li>This field is required.</li></ul>Last name: <input type="text" name="last_name" /></li>
<li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li> <li><ul class="errorlist"><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
If you don't pass any values to the Form's __init__(), or if you pass None, If you don't pass any values to the Form's __init__(), or if you pass None,
the Form won't do any validation. Form.errors will be an empty dictionary *but* the Form won't do any validation. Form.errors will be an empty dictionary *but*
@ -1445,11 +1445,11 @@ Form.clean() is required to return a dictionary of all clean data.
{} {}
>>> f = UserRegistration({}) >>> f = UserRegistration({})
>>> print f.as_table() >>> print f.as_table()
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Username:</td><td><input type="text" name="username" /></td></tr> <tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr> <tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr>
<tr><td colspan="2"><ul><li>This field is required.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>This field is required.</li></ul></td></tr>
<tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr> <tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr>
>>> f.errors >>> f.errors
{'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']} {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
@ -1457,12 +1457,12 @@ Form.clean() is required to return a dictionary of all clean data.
>>> f.errors >>> f.errors
{'__all__': [u'Please make sure your passwords match.']} {'__all__': [u'Please make sure your passwords match.']}
>>> print f.as_table() >>> print f.as_table()
<tr><td colspan="2"><ul><li>Please make sure your passwords match.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
<tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr> <tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr> <tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr> <tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>
>>> print f.as_ul() >>> print f.as_ul()
<li><ul><li>Please make sure your passwords match.</li></ul></li> <li><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></li>
<li>Username: <input type="text" name="username" value="adrian" /></li> <li>Username: <input type="text" name="username" value="adrian" /></li>
<li>Password1: <input type="password" name="password1" value="foo" /></li> <li>Password1: <input type="password" name="password1" value="foo" /></li>
<li>Password2: <input type="password" name="password2" value="bar" /></li> <li>Password2: <input type="password" name="password2" value="bar" /></li>
@ -1556,8 +1556,8 @@ Case 2: POST with erroneous data (a redisplayed form, with errors).
>>> print my_function('POST', {'username': 'this-is-a-long-username', 'password1': 'foo', 'password2': 'bar'}) >>> print my_function('POST', {'username': 'this-is-a-long-username', 'password1': 'foo', 'password2': 'bar'})
<form action="" method="post"> <form action="" method="post">
<table> <table>
<tr><td colspan="2"><ul><li>Please make sure your passwords match.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
<tr><td colspan="2"><ul><li>Ensure this value has at most 10 characters.</li></ul></td></tr> <tr><td colspan="2"><ul class="errorlist"><li>Ensure this value has at most 10 characters.</li></ul></td></tr>
<tr><td>Username:</td><td><input type="text" name="username" value="this-is-a-long-username" /></td></tr> <tr><td>Username:</td><td><input type="text" name="username" value="this-is-a-long-username" /></td></tr>
<tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr> <tr><td>Password1:</td><td><input type="password" name="password1" value="foo" /></td></tr>
<tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr> <tr><td>Password2:</td><td><input type="password" name="password2" value="bar" /></td></tr>