[Fixed]-Django – form Clean() and field errors

14👍

New in Django 1.7 is Form.add_error( field, message ).

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

8👍

Maybe this will help you . Its generally preferred you override clean and inside the function you could do the following

If you want to raise form specific errors you could do .

self._errors["field"] = ErrorList([u"Error"])

this is make sure you get the error class

if you have an non field error you could simple raise a validation error like so

raise forms.ValidationError(_("Error"))

Hope this helps.

6👍

  1. Standard way is raise ValidationError(message).
  2. Move field-specific validation to clean_<fieldname>() methods, clean_address in your case. ValidationError raised in such method will attach error message to specific field. One raised from clean() will be attributed to model in general.

Leave a comment