98👍
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.utils import ErrorList
errors = form._errors.setdefault("myfield", ErrorList())
errors.append(u"My error here")
And if you want to add non-field errors, use django.forms.forms.NON_FIELD_ERRORS
(defaults to "__all__"
) instead of "myfield"
.
135👍
For Django 1.7+, you should use form.add_error()
instead of accessing form._errors
directly.
Documentation: https://docs.djangoproject.com/en/stable/ref/forms/api/#django.forms.Form.add_error
- [Django]-Django form fails validation on a unique field
- [Django]-How to get GET request values in Django?
- [Django]-OneToOneField() vs ForeignKey() in Django
13👍
You can add additional error details to the form’s _errors
attribute directly:
https://docs.djangoproject.com/en/1.5/ref/forms/validation/#described-later
https://docs.djangoproject.com/en/1.6/ref/forms/validation/#modifying-field-errors
- [Django]-Is this the right way to do dependency injection in Django?
- [Django]-What is the best django model field to use to represent a US dollar amount?
- [Django]-How to properly use the "choices" field option in Django
10👍
Add error to specific field :
form.add_error('fieldName', 'error description')
**Add error to non fields **
form.add_error(None, 'error description')
#Only pass None instead of field name
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Substring in a django template?
- [Django]-How to customize activate_url on django-allauth?