Opened 5 months ago

Closed 5 months ago

#35243 closed New feature (wontfix)

Add easy access to error codes in forms

Reported by: Christophe Henry Owned by: nobody
Component: Forms Version: 5.0
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 (last modified by Christophe Henry)

The ValidationError has a code field that can be used in views to determined what exactly failed during form validation. Unfortunately, the list of error codes in not easily accessible from form. Actually, if I want to recover the ValidationError.codes for a form's particular field, I must write the following:

error_codes = [err.code for err in form.errors.get("field", ErrorList()).data]

I propose to add an error_codes property to the Form class as follows:

@property
def errors_codes(self):
    """Return the list of error codes for each field of this form."""
    if self._errors_codes is None:
        self._errors_codes = {
            field: [error.code for error in errors.data if error.code]
            for field, errors in self.errors.items()
        }
    return self._errors_codes

Change History (6)

comment:1 by Christophe Henry, 5 months ago

Component: UncategorizedForms
Type: UncategorizedNew feature

comment:2 by Christophe Henry, 5 months ago

Description: modified (diff)

comment:3 by Christophe Henry, 5 months ago

Description: modified (diff)

comment:4 by Christophe Henry, 5 months ago

Description: modified (diff)

comment:5 by Christophe Henry, 5 months ago

Description: modified (diff)

comment:6 by Natalia Bidart, 5 months ago

Resolution: wontfix
Status: newclosed

Hello! Thank you for your report and interest in making Django better.

As you shown in your example, this is trivial to add to your code base, and it seems a very specific need arising from a niche use case. I don't think this applies to the broader ecosystem, and Django is a framework designed to offer robust and accurate solutions for common scenarios.

Given the above, I'll close the ticket accordingly, but if you disagree, you can consider starting a new conversation on the Django Forum, where you'll reach a wider audience and likely get extra feedback. More information in the documented guidelines for requesting features.

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