JayPrime2012
JayPrime2012

Reputation: 2692

Django ValidationError - how to use this properly?

Currently there is code that is doing (from with the form):

# the exception that gets raised if the form is not valid
raise forms.ValidationError("there was an error");

# here is where form.is_valid is called
form.is_valid() == False: 

    response['msg']=str(form.errors)
    response['err']='row not updated.'
    json = simplejson.dumps( response )  #this json will get returned from the view.

The problem with this, is that it is sending err message to the client as:

  • __all__
    • "There was an error."
  • I want to remove the "all" garbage from the error template that is returned. How can I go about doing this? it seems to get added deep in django form code.

    Upvotes: 0

    Views: 1782

    Answers (2)

    Peter DeGlopper
    Peter DeGlopper

    Reputation: 37319

    errors is an instance of a subclass of dict with some special rendering code. Most of the keys are the fields of the form, but as the docs describe, raising ValidationError in clean produces an error message that isn't associated with any particular field:

    Note that any errors raised by your Form.clean() override will not be associated with any field in particular. They go into a special “field” (called __all__), which you can access via the non_field_errors() method if you need to. If you want to attach errors to a specific field in the form, you will need to access the _errors attribute on the form, which is described later.

    https://docs.djangoproject.com/en/dev/ref/forms/validation/

    So you can either generate your string representation of the errors differently (probably starting with form.errors.values() or form.errors.itervalues(), and maybe using the as_text method of the default ErrorList class) or associate your error with a particular field of the form as described in the docs:

    When you really do need to attach the error to a particular field, you should store (or amend) a key in the Form._errors attribute. This attribute is an instance of a django.forms.utils.ErrorDict class. Essentially, though, it’s just a dictionary. There is a key in the dictionary for each field in the form that has an error. Each value in the dictionary is a django.forms.utils.ErrorList instance, which is a list that knows how to display itself in different ways. So you can treat _errors as a dictionary mapping field names to lists.

    If you want to add a new error to a particular field, you should check whether the key already exists in self._errors or not. If not, create a new entry for the given key, holding an empty ErrorList instance. In either case, you can then append your error message to the list for the field name in question and it will be displayed when the form is displayed.

    https://docs.djangoproject.com/en/dev/ref/forms/validation/#form-subclasses-and-modifying-field-errors

    Upvotes: 1

    knaperek
    knaperek

    Reputation: 2233

    It's because the error is not associated with any field in particular, but it's so called non-field error.

    If you're only interested in non-field errors, just simply pass this to the response:

    response['msg']=str(form.errors['__all__'])
    

    Upvotes: 2

    Related Questions