views:

717

answers:

2

I'm having an issue getting validation error messages to display for a particular field in a Django form, where the field in question is a ModelMultipleChoiceField.

In the clean(self) method for the Form, I try to add the error message to the field like so:

msg = 'error'
self._errors['field_name'] = ErrorList([msg])
raise forms.ValidationError(msg)

This works okay where 'field_name' points to other field types, but for ModelMultipleChoiceField it just won't display. Should this be handled differently?

A: 

Why are you instantiating an ErrorList and writing to self._errors directly? Calling "raise forms.ValidationError(msg)" takes care of all that already.

And what does your template look like?

lawrence
+1  A: 

Yeah, it sounds like you're doing it wrong.

You should be using the clean_ method instead. Read through that whole document, in fact - it's very informative.

SmileyChris