[Django] #35243: Add easy access to error codes in forms

20 views
Skip to first unread message

Django

unread,
Feb 21, 2024, 8:23:55 AM2/21/24
to django-...@googlegroups.com
#35243: Add easy access to error codes in forms
--------------------------------------------+------------------------
Reporter: Christophe Henry | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 21, 2024, 8:24:27 AM2/21/24
to django-...@googlegroups.com
#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
----------------------------------+--------------------------------------
Changes (by Christophe Henry):

* component: Uncategorized => Forms
* type: Uncategorized => New feature

--
Ticket URL: <https://code.djangoproject.com/ticket/35243#comment:1>

Django

unread,
Feb 21, 2024, 8:25:08 AM2/21/24
to django-...@googlegroups.com
#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

unread,
Feb 21, 2024, 8:25:28 AM2/21/24
to django-...@googlegroups.com

Django

unread,
Feb 21, 2024, 8:26:31 AM2/21/24
to django-...@googlegroups.com

Django

unread,
Feb 21, 2024, 8:26:55 AM2/21/24
to django-...@googlegroups.com
I propose to add an `error_codes` property 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:5>

Django

unread,
Feb 21, 2024, 8:32:42 AM2/21/24
to django-...@googlegroups.com
#35243: Add easy access to error codes in forms
----------------------------------+--------------------------------------
Reporter: Christophe Henry | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 5.0
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* resolution: => wontfix
* status: new => closed

Comment:

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
[https://forum.djangoproject.com/c/internals/5 Django Forum], where you'll
reach a wider audience and likely get extra feedback. More information in
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features the documented guidelines for requesting
features].
--
Ticket URL: <https://code.djangoproject.com/ticket/35243#comment:6>
Reply all
Reply to author
Forward
0 new messages