+
+Test that you can append errors in the clean() method of a Form. This way you can place
+the error message near a real Field (and not __all__)
+>>> class StartEnd(Form):
+... start = IntegerField()
+... end = IntegerField()
+... def clean(self):
+... start, end = self.cleaned_data.get('start'), self.cleaned_data.get('end')
+... if start!=None and end!=None and start>end:
+... self.errors.append('start', u'start must be smaller than end')
+... return self.cleaned_data
+>>> f = StartEnd({'start': '2', 'end': '1'})
+>>> f.is_valid()
+False
+>>> f.errors.get('start')
+[u'start must be smaller than end']
+>>> f = StartEnd({'start': '2'})
+>>> f.is_valid()
+False
+>>> type(f.errors.get('start'))
+
+>>> f = StartEnd({'start': '1', 'end': '2'})
+>>> f.is_valid()
+True
"""
Index: django/newforms/util.py
===================================================================
--- django/newforms/util.py (Revision 6454)
+++ django/newforms/util.py (Arbeitskopie)
@@ -27,6 +27,13 @@
def as_text(self):
return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % force_unicode(i) for i in v])) for k, v in self.items()])
+ def append(self, name, message):
+ errorlist=self.get(name)
+ if errorlist==None:
+ errorlist=ErrorList()
+ self[name]=errorlist
+ errorlist.append(message)
+
class ErrorList(list, StrAndUnicode):
"""
A collection of errors that knows how to display itself in various formats.
Index: docs/newforms.txt
===================================================================
--- docs/newforms.txt (Revision 6454)
+++ docs/newforms.txt (Arbeitskopie)
@@ -1460,6 +1460,12 @@
"field" (called ``__all__``, which you can access via the
``non_field_errors()`` method if you need to.
+
+ Instead of raising an ValidationError inside ``Form.clean()``,
+ you can add an error message with
+ ``self.errors.append(fieldname, message)``. This way the error message
+ will be displayed near the given field.
+
These methods are run in the order given above, one field at a time. That is,
for each field in the form (in the order they are declared in the form
definition), the ``Field.clean()`` method (or it's override) is run, then