From b4a67359a34c77e2e40f1ab8e30b96278a063007 Mon Sep 17 00:00:00 2001
From: Adrian Holovaty s."
- top_errors = self.non_field_errors()
- output, hidden_fields = [], []
- for name, field in self.fields.items():
- bf = BoundField(self, field, name)
- bf_errors = bf.errors # Cache in local variable.
- if bf.is_hidden:
- if bf_errors:
- top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
- hidden_fields.append(unicode(bf))
- else:
- if bf_errors:
- output.append(u' %s %s %s %s .
- str_hidden = u''.join(hidden_fields)
- if output:
- last_td = output[-1]
- # Chop off the trailing 's -- excluding the ."
- top_errors = self.non_field_errors()
+ def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row):
+ "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.
output, hidden_fields = [], []
for name, field in self.fields.items():
bf = BoundField(self, field, name)
@@ -84,72 +84,36 @@ class Form(StrAndUnicode):
top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors])
hidden_fields.append(unicode(bf))
else:
- if bf_errors:
- output.append(u'
' % bf_errors)
- output.append(u'%s ' % (bf.label_tag(escape(bf.verbose_name+':')), bf))
+ label = bf.label_tag(escape(bf.verbose_name+':'))
+ if errors_on_separate_row:
+ if bf_errors:
+ output.append(error_row % bf_errors)
+ output.append(normal_row % (label, bf))
+ else:
+ output.append(normal_row % ((bf_errors, label, bf)))
if top_errors:
- output.insert(0, u'%s %s ' % top_errors)
- if hidden_fields: # Insert any hidden fields in the last %s .
+ output.insert(0, error_row % top_errors)
+ if hidden_fields: # Insert any hidden fields in the last row.
str_hidden = u''.join(hidden_fields)
if output:
- last_td = output[-1]
- # Chop off the trailing ' ' and insert the hidden fields.
- output[-1] = last_td[:-10] + str_hidden + ''
- else: # If there aren't any ''s in the output, just append the hidden fields.
+ last_row = output[-1]
+ # Chop off the trailing row_ender (e.g. ' ') and insert the hidden fields.
+ output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
+ else: # If there aren't any rows in the output, just append the hidden fields.
output.append(str_hidden)
return u'\n'.join(output)
+ def as_table(self):
+ "Returns this form rendered as HTML s -- excluding the ."
+ return self._html_output(u'
', u'%s %s ', '', True)
+
def as_ul(self):
"Returns this form rendered as HTML %s ."
- top_errors = self.non_field_errors()
- output, hidden_fields = [], []
- for name, field in self.fields.items():
- bf = BoundField(self, field, name)
- if bf.is_hidden:
- new_errors = bf.errors # Cache in local variable.
- if new_errors:
- top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors])
- hidden_fields.append(unicode(bf))
- else:
- output.append(u'
's in the output, just append the hidden fields. - output.append(str_hidden) - return u'\n'.join(output) + return self._html_output(u'
%s %s
', u'%s
', '', True) def non_field_errors(self): """