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:1 by , 4 years ago
Has patch: | set |
---|
comment:2 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 4 years ago
Resolution: | → needsinfo |
---|---|
Status: | assigned → closed |
Triage Stage: | Accepted → Unreviewed |
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 , 4 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
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:6 by , 4 years ago
Needs tests: | set |
---|
comment:7 by , 4 years ago
Needs tests: | unset |
---|
comment:8 by , 4 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
https://github.com/django/django/pull/13212