Login

Getting the global error of a form

Author:
baptiste
Posted:
May 19, 2007
Language:
Python
Version:
.96
Tags:
newforms forms
Score:
1 (after 1 ratings)

You may want to access to "global" errors in your template, typically when you have a custom clean() method which checks several fields. {{ form.errors }} gives an < ul >, which is often not what you want, and {{ form.errors|join:", " }} iterates in the keys of errors (which are the names of fields, uninteresting). The global errors's key is "all", and Django doesn't allow us to do {{ form.errors.__all__ }}.

This method returns an array, like a classic {{ form.field.errors }}, and can be used with a join : {{ form.get_global_errors|join:", " }}.

1
2
3
4
5
class MyForm(forms.Form):

    def get_global_errors(self):
        errors = dict(self.errors)
        return list(errors.get("__all__", []))

More like this

  1. AjaxForm Base Classes by btaylordesign 4 years, 4 months ago
  2. Validation for 'unique' and 'unique_together' constraints (different version) by miracle2k 8 years ago
  3. Querying on existence of a relationship by ubernostrum 8 years ago
  4. List all errors in a form +bootstrap highlighting by ibrahimlawal 2 years, 10 months ago
  5. Convert an instance to a dictionary for use in newforms by SmileyChris 8 years, 3 months ago

Comments

justinw (on May 19, 2007):

def get_global_errors(self): return self.errors and self.errors.get('_all_',[])

#

Archatas (on September 12, 2008):

I am not sure about the situation at the time of writing this post, but global errors can be accessed by {{ form.non_field_errors }}

#

Please login first before commenting.