tags:

views:

706

answers:

2

After my form.Form validates the user input values I pass them to a separate (external) process for further processing. This external process can potentially find further errors in the values.

Is there a way to inject these errors into the already validated form so they can be displayed via the usual form error display methods? Or, are there better alternative approaches?

One suggestions was to include the external processing in the form validation. I'm not crazy about this since the external process does a lot more than just validate.

+4  A: 

You can add additional error details to the form's _errors attribute directly:

http://docs.djangoproject.com/en/dev/ref/forms/validation/#described-later

insin
+6  A: 

Form._errors can be treated like a standard dictionary. It's considered good form to use the ErrorList class, and to append errors to the existing list:

from django.forms.util import ErrorList
errors = form._errors.setdefault("myfield", ErrorList())
errors.append(u"My error here")
John Millikin
To match what Django's doing, you should insert ErrorList objects.
scompt.com
This seems a little bit unpleasant, since the name "_errors" suggests it's supposed to be internal to the form class. Is that the standard way to do this in Django? I'm in a similar situation to the OP: I have a form where users provide a new name that must be unique in the database. If there's a collision I'd like to send the form back with an error on it, but I won't know until I actually try to do the database insert. (In theory the validator could check the database, but that smells and is race-prone.)
Weeble
@scompt: thanks, fixed. @Weeble: `_errors` is part of the public form API, despite its name; see insin's answer for a docs link.
John Millikin