Opened 6 hours ago

Last modified 6 hours ago

#37244 new Bug

Interaction between postgres ArrayField model field and ModelForm results in incomplete validation error messages

Reported by: Chris Owned by:
Component: Forms Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The contrib.postgres model ArrayField uses the validator(s) of the base (model) field of the ArrayField to validate the items in the array. It catches the ValidationError exceptions, then prefixes them with its own prefix ("Item <x> in the array did not validate:") and re-raises this exception with the `"item_invalid"` code.

BaseModelForm collects these model validation errors in the _post_clean() method, and passes them to _update_errors(). This last method replaces the message of a validation error if the code of that validation error also is in the form field's error_messages dict, before adding it to the form errors.

The problem here is of course that the helpful message from the model (i.e. "Item <x> in the array did not validate: <reason from base field validator>") gets replaced by only the prefix: "Item <x> in the array did not validate:", and this is what is then rendered as the error in the form, with the entire reason for the validation failure no longer shown.

This is not usually a problem if the model field also has a corresponding custom form field which has the same validators, because these are run first and should catch the same problems before the model validators are tried, and these are not subject to the same replace-validation-message-from-model-with-the-one-from-the-form mechanics.

Change History (1)

comment:1 by Yassin Bahri, 6 hours ago

If this gets triaged and accepted can I work on it if possible?
Thanks

Note: See TracTickets for help on using tickets.
Back to Top