Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 682e435c5f334bc0211b12f05398fe79e4e72713 1 parent 48b36bb
Adrian Holovaty authored November 27, 2006
17  django/newforms/forms.py
@@ -56,19 +56,20 @@ def __getitem__(self, name):
56 56
             raise KeyError('Key %r not found in Form' % name)
57 57
         return BoundField(self, field, name)
58 58
 
59  
-    def errors(self):
  59
+    def _errors(self):
60 60
         "Returns an ErrorDict for self.data"
61 61
         if self.__errors is None:
62 62
             self.full_clean()
63 63
         return self.__errors
  64
+    errors = property(_errors)
64 65
 
65 66
     def is_valid(self):
66 67
         """
67 68
         Returns True if the form has no errors. Otherwise, False. This exists
68 69
         solely for convenience, so client code can use positive logic rather
69  
-        than confusing negative logic ("if not form.errors()").
  70
+        than confusing negative logic ("if not form.errors").
70 71
         """
71  
-        return not bool(self.errors())
  72
+        return not bool(self.errors)
72 73
 
73 74
     def as_table(self):
74 75
         "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
@@ -81,9 +82,9 @@ def as_ul(self):
81 82
     def as_table_with_errors(self):
82 83
         "Returns this form rendered as HTML <tr>s, with errors."
83 84
         output = []
84  
-        if self.errors().get(NON_FIELD_ERRORS):
  85
+        if self.errors.get(NON_FIELD_ERRORS):
85 86
             # Errors not corresponding to a particular field are displayed at the top.
86  
-            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]]))
  87
+            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]]))
87 88
         for name, field in self.fields.items():
88 89
             bf = BoundField(self, field, name)
89 90
             if bf.errors:
@@ -94,9 +95,9 @@ def as_table_with_errors(self):
94 95
     def as_ul_with_errors(self):
95 96
         "Returns this form rendered as HTML <li>s, with errors."
96 97
         output = []
97  
-        if self.errors().get(NON_FIELD_ERRORS):
  98
+        if self.errors.get(NON_FIELD_ERRORS):
98 99
             # Errors not corresponding to a particular field are displayed at the top.
99  
-            output.append(u'<li><ul>%s</ul></li>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors()[NON_FIELD_ERRORS]]))
  100
+            output.append(u'<li><ul>%s</ul></li>' % u'\n'.join([u'<li>%s</li>' % e for e in self.errors[NON_FIELD_ERRORS]]))
100 101
         for name, field in self.fields.items():
101 102
             bf = BoundField(self, field, name)
102 103
             line = u'<li>'
@@ -162,7 +163,7 @@ def _errors(self):
162 163
         if there are none.
163 164
         """
164 165
         try:
165  
-            return self._form.errors()[self._name]
  166
+            return self._form.errors[self._name]
166 167
         except KeyError:
167 168
             return ErrorList()
168 169
     errors = property(_errors)
24  tests/regressiontests/forms/tests.py
@@ -1163,13 +1163,13 @@
1163 1163
 <li><ul><li>This field is required.</li></ul>Birthday: <input type="text" name="birthday" /></li>
1164 1164
 
1165 1165
 >>> p = Person({'first_name': u'John', 'last_name': u'Lennon', 'birthday': u'1940-10-9'})
1166  
->>> p.errors()
  1166
+>>> p.errors
1167 1167
 {}
1168 1168
 >>> p.is_valid()
1169 1169
 True
1170  
->>> p.errors().as_ul()
  1170
+>>> p.errors.as_ul()
1171 1171
 u''
1172  
->>> p.errors().as_text()
  1172
+>>> p.errors.as_text()
1173 1173
 u''
1174 1174
 >>> p.clean()
1175 1175
 {'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
@@ -1197,13 +1197,13 @@
1197 1197
 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>'
1198 1198
 
1199 1199
 >>> p = Person({'last_name': u'Lennon'})
1200  
->>> p.errors()
  1200
+>>> p.errors
1201 1201
 {'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
1202 1202
 >>> p.is_valid()
1203 1203
 False
1204  
->>> p.errors().as_ul()
  1204
+>>> p.errors.as_ul()
1205 1205
 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>'
1206  
->>> print p.errors().as_text()
  1206
+>>> print p.errors.as_text()
1207 1207
 * first_name
1208 1208
   * This field is required.
1209 1209
 * birthday
@@ -1385,13 +1385,13 @@
1385 1385
 ...            raise ValidationError(u'Please make sure your passwords match.')
1386 1386
 ...        return self.clean_data['password2']
1387 1387
 >>> f = UserRegistration()
1388  
->>> f.errors()
  1388
+>>> f.errors
1389 1389
 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
1390 1390
 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
1391  
->>> f.errors()
  1391
+>>> f.errors
1392 1392
 {'password2': [u'Please make sure your passwords match.']}
1393 1393
 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
1394  
->>> f.errors()
  1394
+>>> f.errors
1395 1395
 {}
1396 1396
 >>> f.clean()
1397 1397
 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
@@ -1414,10 +1414,10 @@
1414 1414
 <tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
1415 1415
 <tr><td>Password1:</td><td><input type="password" name="password1" /></td></tr>
1416 1416
 <tr><td>Password2:</td><td><input type="password" name="password2" /></td></tr>
1417  
->>> f.errors()
  1417
+>>> f.errors
1418 1418
 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
1419 1419
 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'})
1420  
->>> f.errors()
  1420
+>>> f.errors
1421 1421
 {'__all__': [u'Please make sure your passwords match.']}
1422 1422
 >>> print f.as_table()
1423 1423
 <tr><td>Username:</td><td><input type="text" name="username" value="adrian" /></td></tr>
@@ -1434,7 +1434,7 @@
1434 1434
 <li>Password1: <input type="password" name="password1" value="foo" /></li>
1435 1435
 <li>Password2: <input type="password" name="password2" value="bar" /></li>
1436 1436
 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'})
1437  
->>> f.errors()
  1437
+>>> f.errors
1438 1438
 {}
1439 1439
 >>> f.clean()
1440 1440
 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}

0 notes on commit 682e435

Please sign in to comment.
Something went wrong with that request. Please try again.