Error entering an already approved form?

After my form.Form checks the values โ€‹โ€‹entered by the user, I pass them to a separate (external) process for further processing. This external process can potentially find additional errors in the values.

Is there a way to incorporate these errors into an already verified form so that they can be displayed using the usual methods for displaying form errors (or are there better alternative approaches)?

One suggestion was to include external processing in form validation, which is not ideal because the external process does much more than just validation.

+86
django django-forms
09 Oct '08 at 19:45
source share
4 answers

Form._errors can be thought of as a standard dictionary. It would be nice to use the ErrorList class and add errors to the existing list:

 from django.forms.utils import ErrorList errors = form._errors.setdefault("myfield", ErrorList()) errors.append(u"My error here") 

And if you want to add django.forms.forms.NON_FIELD_ERRORS errors, use django.forms.forms.NON_FIELD_ERRORS (default is "__all__" ) instead of "myfield" .

+86
09 Oct '08 at 19:49
source share

For Django 1.7+, you should use form.add_error() instead of directly accessing form._errors .

Documentation: https://docs.djangoproject.com/en/stable/ref/forms/api/#django.forms.Form.add_error

+89
Jan 21 '15 at 1:45
source share
+14
Oct 09 '08 at 19:48
source share

The accepted answer is correct, but similar to the Django 1.5 ErrorList located in the util.py file util.py Thus, the import will look like this from django.forms.util import ErrorList .

0
Feb 06 '19 at 16:00
source share



All Articles