newforms: Changed Form.errors to be a property rather than a function. Refs #3026
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4116 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
48b36bb4a4
commit
682e435c5f
|
@ -56,19 +56,20 @@ class Form(object):
|
||||||
raise KeyError('Key %r not found in Form' % name)
|
raise KeyError('Key %r not found in Form' % name)
|
||||||
return BoundField(self, field, name)
|
return BoundField(self, field, name)
|
||||||
|
|
||||||
def errors(self):
|
def _errors(self):
|
||||||
"Returns an ErrorDict for self.data"
|
"Returns an ErrorDict for self.data"
|
||||||
if self.__errors is None:
|
if self.__errors is None:
|
||||||
self.full_clean()
|
self.full_clean()
|
||||||
return self.__errors
|
return self.__errors
|
||||||
|
errors = property(_errors)
|
||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
"""
|
"""
|
||||||
Returns True if the form has no errors. Otherwise, False. This exists
|
Returns True if the form has no errors. Otherwise, False. This exists
|
||||||
solely for convenience, so client code can use positive logic rather
|
solely for convenience, so client code can use positive logic rather
|
||||||
than confusing negative logic ("if not form.errors()").
|
than confusing negative logic ("if not form.errors").
|
||||||
"""
|
"""
|
||||||
return not bool(self.errors())
|
return not bool(self.errors)
|
||||||
|
|
||||||
def as_table(self):
|
def as_table(self):
|
||||||
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
|
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
|
||||||
|
@ -81,9 +82,9 @@ class Form(object):
|
||||||
def as_table_with_errors(self):
|
def as_table_with_errors(self):
|
||||||
"Returns this form rendered as HTML <tr>s, with errors."
|
"Returns this form rendered as HTML <tr>s, with errors."
|
||||||
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"><ul>%s</ul></td></tr>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors[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:
|
||||||
|
@ -94,9 +95,9 @@ class Form(object):
|
||||||
def as_ul_with_errors(self):
|
def as_ul_with_errors(self):
|
||||||
"Returns this form rendered as HTML <li>s, with errors."
|
"Returns this form rendered as HTML <li>s, with errors."
|
||||||
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><ul>%s</ul></li>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors[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>'
|
||||||
|
@ -162,7 +163,7 @@ class BoundField(object):
|
||||||
if there are none.
|
if there are none.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return self._form.errors()[self._name]
|
return self._form.errors[self._name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ErrorList()
|
return ErrorList()
|
||||||
errors = property(_errors)
|
errors = property(_errors)
|
||||||
|
|
|
@ -1163,13 +1163,13 @@ u''
|
||||||
<li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
|
<li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
|
||||||
|
|
||||||
>>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
|
>>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
|
||||||
>>> p.errors()
|
>>> p.errors
|
||||||
{}
|
{}
|
||||||
>>> p.is_valid()
|
>>> p.is_valid()
|
||||||
True
|
True
|
||||||
>>> p.errors().as_ul()
|
>>> p.errors.as_ul()
|
||||||
u''
|
u''
|
||||||
>>> p.errors().as_text()
|
>>> p.errors.as_text()
|
||||||
u''
|
u''
|
||||||
>>> p.clean()
|
>>> p.clean()
|
||||||
{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
|
{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
|
||||||
|
@ -1197,13 +1197,13 @@ u'<tr><td>First name:</td><td><input type="text" name="first_name" value="John"
|
||||||
u'<li>First name: <input type="text" name="first_name" value="John" /></li>\n<li>Last name: <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /></li>\n<li>Birthday: <input type="text" name="birthday" /></li>'
|
u'<li>First name: <input type="text" name="first_name" value="John" /></li>\n<li>Last name: <input type="text" name="last_name" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /></li>\n<li>Birthday: <input type="text" name="birthday" /></li>'
|
||||||
|
|
||||||
>>> p = Person({'last_name': u'Lennon'})
|
>>> p = Person({'last_name': u'Lennon'})
|
||||||
>>> p.errors()
|
>>> p.errors
|
||||||
{'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
|
{'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
|
||||||
>>> p.is_valid()
|
>>> p.is_valid()
|
||||||
False
|
False
|
||||||
>>> p.errors().as_ul()
|
>>> p.errors.as_ul()
|
||||||
u'<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>'
|
u'<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>'
|
||||||
>>> print p.errors().as_text()
|
>>> print p.errors.as_text()
|
||||||
* first_name
|
* first_name
|
||||||
* This field is required.
|
* This field is required.
|
||||||
* birthday
|
* birthday
|
||||||
|
@ -1385,13 +1385,13 @@ Field.clean(), the clean_XXX() method should return the cleaned value:
|
||||||
... raise ValidationError(u'Please make sure your passwords match.')
|
... raise ValidationError(u'Please make sure your passwords match.')
|
||||||
... return self.clean_data['password2']
|
... return self.clean_data['password2']
|
||||||
>>> f = UserRegistration()
|
>>> f = UserRegistration()
|
||||||
>>> 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.']}
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
||||||
>>> f.errors()
|
>>> f.errors
|
||||||
{'password2': [u'Please make sure your passwords match.']}
|
{'password2': [u'Please make sure your passwords match.']}
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
||||||
>>> f.errors()
|
>>> f.errors
|
||||||
{}
|
{}
|
||||||
>>> f.clean()
|
>>> f.clean()
|
||||||
{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
|
{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
|
||||||
|
@ -1414,10 +1414,10 @@ Form.clean() still needs to return a dictionary of all clean data:
|
||||||
<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>Password1:</td><td><input type="password" name="password1" /></td></tr>
|
<tr><td>Password1:</td><td><input type="password" name="password1" /></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.']}
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
|
||||||
>>> 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>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>
|
||||||
|
@ -1434,7 +1434,7 @@ Form.clean() still needs to return a dictionary of all clean data:
|
||||||
<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>
|
||||||
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
|
||||||
>>> f.errors()
|
>>> f.errors
|
||||||
{}
|
{}
|
||||||
>>> f.clean()
|
>>> f.clean()
|
||||||
{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
|
{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
|
||||||
|
|
Loading…
Reference in New Issue