[Fixed]-Adding errors to Django form errors.__all__

18👍

You really want to do this during form validation and raise a ValidationError from there… but if you’re set on doing it this way you’ll want to access _errors to add new messages. Try something like this:

from django.forms.util import ErrorList

our_form._errors["field_name"] = ErrorList([u"error message goes here"])

12👍

Non field errors can be added using the constant NON_FIELD_ERRORS dictionary key (which is __all__ by default):

from django import forms
errors = my_form._errors.setdefault(forms.forms.NON_FIELD_ERRORS, forms.util.ErrorList())
errors.append("My error here")

12👍

In Django 1.7 or higher, I would do:

form.add_error(field_name, "Some message")

The method add_error was added in 1.7. The form variable is the form I want to manipulate and field_name is the specific field name or None if I want an error that is not associated with a specific field.

In Django 1.6 I would do something like:

from django.forms.forms import NON_FIELD_ERRORS

errors = form._errors.setdefault(field_name, form.error_class())
errors.append("Some message")

In the code above form is the form I want to manipulate and field_name is the field name for which I want to add an error. field_name can be set to NON_FIELD_ERRORS to add an error not associated with a specific field. I use form.error_class() to generate the empty list of error messages. This is how Django 1.6 internally creates an empty list rather than instantiate ErrorList() directly.

👤Louis

9👍

You should raise the validationerror.

Why not put the verification within the form’s clean method

class ProfileForm(forms.Form):
    def clean(self):
        try:
            #Make a call to the API and verify it works well
        except:
            raise forms.ValidationError('Your address is not locatable by Google Maps')

that way, you just need the standard form.is_valid() in the view.

👤lprsd

8👍

You’re almost there with your original solution. Here is a base Form class I built which allows me to do the same thing, i.e. add non-field error messages to the form:

from django import forms
from django.forms.util import ErrorDict
from django.forms.forms import NON_FIELD_ERRORS 

class MyBaseForm(forms.Form):
    def add_form_error(self, message):
        if not self._errors:
            self._errors = ErrorDict()
        if not NON_FIELD_ERRORS in self._errors:
            self._errors[NON_FIELD_ERRORS] = self.error_class()
        self._errors[NON_FIELD_ERRORS].append(message)

class MyForm(MyBaseForm):
    ....

All my forms extend this class and so I can simply call the add_form_error() method to add another error message.

2👍

I’m not sure how horrible of a hack this is (I’ve only really worked on two Django projects up until this point) but if you do something like follows you get a separate error message that is not associated with a specific field in the model:

form = NewPostForm()
if something_went_horribly_wrong():
  form.errors[''] = "You broke it!"
👤Geoff

0👍

If the validation pertains to the data layer, then you should indeed not use form validation. Since Django 1.2 though, there exists a similar concept for Django models and this is certainly what you shoud use. See the documentation for model validation.

👤Lloeki

Leave a comment