[Django] #31806: Make validators include the provided value in ValidationError

12 views
Skip to first unread message

Django

unread,
Jul 20, 2020, 10:52:37 PM7/20/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
----------------------------------------+------------------------
Reporter: Jon Dufresne | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
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 |
----------------------------------------+------------------------
It is sometimes desirable to include the provide value in a custom error
message. For example:

{{{
“blah” is not a valid email.
}}}

By making built-in validators provide `value` to `ValidationError`, one
can override an error message and use a `%(value)s` placeholder.

This placeholder `value` matches an example already in the docs:

https://docs.djangoproject.com/en/3.0/ref/validators/#writing-validators

--
Ticket URL: <https://code.djangoproject.com/ticket/31806>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 20, 2020, 10:54:35 PM7/20/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+--------------------------------------

Reporter: Jon Dufresne | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by Jon Dufresne):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/13212

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

Django

unread,
Jul 21, 2020, 1:10:46 AM7/21/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by felixxm):

* owner: nobody => Jon Dufresne
* status: new => assigned
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:2>

Django

unread,
Jul 21, 2020, 1:36:47 AM7/21/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by felixxm):

* status: assigned => closed
* resolution: => needsinfo
* stage: Accepted => Unreviewed


Comment:

After reconsideration I have some doubts. Do you think it's really useful
to include invalid values in error messages? Invalid form is redisplayed
with errors in most (all?) of cases, so I'm not sure if it can be useful
to display error messages containing invalid values next to form fields
with invalid values (see
[https://code.djangoproject.com/ticket/15069#comment:9 comment]). I'm
trying to find a use case.

--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:3>

Django

unread,
Jul 21, 2020, 7:12:09 AM7/21/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Jon Dufresne):

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


Comment:

> Invalid form is redisplayed with errors in most (all?) of cases

This assumes the form system is used only for a narrow set of use cases
that involving HTML rendering. This is not always the case, especially in
projects I work on.

The form system has two major features: input validation and HTML
rendering. I often use the validation system without the HTML rendering.
When used in this context, yes, being able to include the provided value
in custom error messages is quite helpful. Sometimes this error is
displayed in a log other times as an alert. I opened this ticket as my
application has practical uses for it, not theoretical. I hit a wall when
trying to change an error message to meet an internal specification.

As a concrete example, my application allows uploading spreadsheets and
the data in those spreadsheets is validated using a Django form. This use
case has a history of support: see #24229. In the event of invalid data,
the precise error can't be displayed next to the originating value.
Providing the submitted value as context in the message helps the user
locate and correct it. For example `Email “blah” in cell A1 is not a valid
email address`.

Beyond my concrete use case, the code change is quite minimal (IMO) and
unobtrusive while providing more flexibility and control for users to
customize error messages. I think we should give users that power as it
comes with a low maintenance cost.

--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:4>

Django

unread,
Jul 21, 2020, 7:19:13 AM7/21/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by felixxm):

* stage: Unreviewed => Accepted


Comment:

Sounds reasonable, thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:5>

Django

unread,
Jul 21, 2020, 7:39:29 AM7/21/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by felixxm):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:6>

Django

unread,
Jul 26, 2020, 11:26:40 PM7/26/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
------------------------------+----------------------------------------
Reporter: Jon Dufresne | Owner: Jon Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Jon Dufresne):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:7>

Django

unread,
Jul 27, 2020, 6:59:45 AM7/27/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
-------------------------------------+-------------------------------------

Reporter: Jon Dufresne | Owner: Jon
| Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:8>

Django

unread,
Jul 27, 2020, 3:02:14 PM7/27/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
| Dufresne
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"013e06bb3795bcf840e29576fda7720e26cd7e01" 013e06bb]:
{{{
#!CommitTicketReference repository=""
revision="013e06bb3795bcf840e29576fda7720e26cd7e01"
Refs #31806 -- Added tests for including the value in some
ValidationErrors.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:9>

Django

unread,
Jul 27, 2020, 3:02:15 PM7/27/20
to django-...@googlegroups.com
#31806: Make validators include the provided value in ValidationError
-------------------------------------+-------------------------------------
Reporter: Jon Dufresne | Owner: Jon
| Dufresne
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"83fbaa92311dd96e330496a0e443ea71b9c183e2" 83fbaa92]:
{{{
#!CommitTicketReference repository=""
revision="83fbaa92311dd96e330496a0e443ea71b9c183e2"
Fixed #31806 -- Made validators include the value in ValidationErrors.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31806#comment:10>

Reply all
Reply to author
Forward
0 new messages