Opened 4 years ago

Closed 4 years ago

#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 by Mariusz Felisiak, 4 years ago

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

comment:3 by Mariusz Felisiak, 4 years ago

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 by Jon Dufresne, 4 years ago

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 by Mariusz Felisiak, 4 years ago

Triage Stage: UnreviewedAccepted

Sounds reasonable, thanks.

comment:6 by Mariusz Felisiak, 4 years ago

Needs tests: set

comment:7 by Jon Dufresne, 4 years ago

Needs tests: unset

comment:8 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 013e06bb:

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

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

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