Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Changed Table.as_table() and Table.as_ul() to put hidden-fo…

…rm errors at the top of the output rather than in field order

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 12e9a84429b2719b00f4eb17d5b80d2c8f19812c 1 parent 558e07a
Adrian Holovaty authored December 07, 2006
28  django/newforms/forms.py
@@ -74,38 +74,38 @@ def is_valid(self):
74 74
 
75 75
     def as_table(self):
76 76
         "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
  77
+        top_errors = self.non_field_errors()
77 78
         output = []
78  
-        if self.errors.get(NON_FIELD_ERRORS):
79  
-            # Errors not corresponding to a particular field are displayed at the top.
80  
-            output.append(u'<tr><td colspan="2">%s</td></tr>' % self.non_field_errors())
81 79
         for name, field in self.fields.items():
82 80
             bf = BoundField(self, field, name)
  81
+            bf_errors = bf.errors
83 82
             if bf.is_hidden:
84  
-                if bf.errors:
85  
-                    new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors])
86  
-                    output.append(u'<tr><td colspan="2">%s</td></tr>' % new_errors)
  83
+                if bf_errors:
  84
+                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
87 85
                 output.append(str(bf))
88 86
             else:
89  
-                if bf.errors:
90  
-                    output.append(u'<tr><td colspan="2">%s</td></tr>' % bf.errors)
  87
+                if bf_errors:
  88
+                    output.append(u'<tr><td colspan="2">%s</td></tr>' % bf_errors)
91 89
                 output.append(u'<tr><td>%s</td><td>%s</td></tr>' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
  90
+        if top_errors:
  91
+            output.insert(0, u'<tr><td colspan="2">%s</td></tr>' % top_errors)
92 92
         return u'\n'.join(output)
93 93
 
94 94
     def as_ul(self):
95 95
         "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
  96
+        top_errors = self.non_field_errors()
96 97
         output = []
97  
-        if self.errors.get(NON_FIELD_ERRORS):
98  
-            # Errors not corresponding to a particular field are displayed at the top.
99  
-            output.append(u'<li>%s</li>' % self.non_field_errors())
100 98
         for name, field in self.fields.items():
101 99
             bf = BoundField(self, field, name)
102 100
             if bf.is_hidden:
103  
-                if bf.errors:
104  
-                    new_errors = ErrorList(['(Hidden field %s) %s' % (name, e) for e in bf.errors])
105  
-                    output.append(u'<li>%s</li>' % new_errors)
  101
+                new_errors = bf.errors # Cache in local variable.
  102
+                if new_errors:
  103
+                    top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors])
106 104
                 output.append(str(bf))
107 105
             else:
108 106
                 output.append(u'<li>%s%s %s</li>' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf))
  107
+        if top_errors:
  108
+            output.insert(0, u'<li>%s</li>' % top_errors)
109 109
         return u'\n'.join(output)
110 110
 
111 111
     def non_field_errors(self):
7  tests/regressiontests/forms/tests.py
@@ -1789,18 +1789,19 @@
1789 1789
 
1790 1790
 If a field with a HiddenInput has errors, the as_table() and as_ul() output
1791 1791
 will include the error message(s) with the text "(Hidden field [fieldname]) "
1792  
-prepended.
  1792
+prepended. This message is displayed at the top of the output, regardless of
  1793
+its field's order in the form.
1793 1794
 >>> p = Person({'first_name': 'John', 'last_name': 'Lennon', 'birthday': '1940-10-9'})
1794 1795
 >>> print p
  1796
+<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
1795 1797
 <tr><td>First name:</td><td><input type="text" name="first_name" value="John" /></td></tr>
1796 1798
 <tr><td>Last name:</td><td><input type="text" name="last_name" value="Lennon" /></td></tr>
1797  
-<tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
1798 1799
 <input type="hidden" name="hidden_text" />
1799 1800
 <tr><td>Birthday:</td><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
1800 1801
 >>> print p.as_ul()
  1802
+<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
1801 1803
 <li>First name: <input type="text" name="first_name" value="John" /></li>
1802 1804
 <li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
1803  
-<li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
1804 1805
 <input type="hidden" name="hidden_text" />
1805 1806
 <li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
1806 1807
 

0 notes on commit 12e9a84

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