#31806 closed New feature (fixed)

Make validators include the provided value in ValidationError

Reported by: Jon Dufresne Owned by: Jon Dufresne
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (10)

comment:2 Changed 18 months ago by Mariusz Felisiak

Owner: changed from nobody to Jon Dufresne
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:3 Changed 18 months ago by Mariusz Felisiak

Resolution: needsinfo
Status: assignedclosed
Triage Stage: AcceptedUnreviewed

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 comment). I'm trying to find a use case.

comment:4 Changed 18 months ago by Jon Dufresne

Resolution: needsinfo
Status: closednew

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.

comment:5 Changed 18 months ago by Mariusz Felisiak

Triage Stage: UnreviewedAccepted

Sounds reasonable, thanks.

comment:6 Changed 18 months ago by Mariusz Felisiak

Needs tests: set

comment:7 Changed 18 months ago by Jon Dufresne

Needs tests: unset

comment:8 Changed 18 months ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:9 Changed 18 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 013e06bb:

Refs #31806 -- Added tests for including the value in some ValidationErrors.

comment:10 Changed 18 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: newclosed

In 83fbaa92:

Fixed #31806 -- Made validators include the value in ValidationErrors.

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