#35243: Add easy access to error codes in forms
----------------------------------+--------------------------------------
Reporter: Christophe Henry | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Description changed by Christophe Henry:
Old description:
> 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.code``s for a form's particular
> field, I must write the following:
>
> {{{#!python
> error_codes = [err.code for err in form.errors.get("field",
> ErrorList()).data]
> }}}
>
> I propose to add a ``error_codes`` to the ``Form`` class as follows:
>
> {{{#!python
> @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
> }}}
New description:
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.code``s for a form's particular
field, I must write the following:
{{{#!python
error_codes = [err.code for err in form.errors.get("field",
ErrorList()).data]
}}}
I propose to add a ``error_codes`` to the ``Form`` class as follows:
{{{#!python
@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
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35243#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018dcbd7562e-3dd50069-515d-49a4-97b4-773ffb1049b7-000000%40eu-central-1.amazonses.com.