ModelForm._post_clean() does not respect Model.clean() ValidationErrors that possess a message_dict, rather than a list of messages
|Reported by:||robboyle||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When a ModelForm runs it's model-level validation, it anticipates the Model's clean_fields() method raising a ValidationError with the message_dict property set. This let's the ModelForm associate the Model's Field validation errors with the ModelForm fields.
However, when the ModelForm then calls the Model's clean() method, it ignores the message_dict property if the ValidationError raised by clean() has it set. Instead, it always creates a new dictionary, associating all validation messages with the NON_FIELD_ERRORS key.
It would be nice if _post_clean() checked if Model.clean() was raising a ValidationError with a message_dict, and if so updated the errors with that dict, rather than blindly creating a new one. That way, the Model.clean() method could associate errors with a particular field, return a ValidationError with message_dict set, and this association would be perserved by the ModelForm.
Being able to have a form's clean method associate errors with a particular field is very handy. Being able to do this at the model level and then have model forms take advantage of it would bring model forms more on par with regular forms.