Opened 14 years ago
Closed 10 years ago
#15069 closed New feature (fixed)
ValidationError message isn't helpful in tracking down the field that fails validation
Reported by: | Owned by: | ANUBHAV JOSHI | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | ValidationError, exception, error message |
Cc: | anubhav9042@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
raise exceptions.ValidationError(self.error_messagesinvalid)
[Wed Jan 12 21:59:58 2011] [error] [client 10.177.133.202] ValidationError: [u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']
If the error message contained the invalid data, and, especially the name of the field that was being validated, that would be way more helpful.
I'm going to go look for this in the source, but someone more familiar with this piece of code could probably make this change in pretty short order.
Change History (13)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Component: | Core framework → Forms |
---|---|
milestone: | 1.3 |
Triage Stage: | Unreviewed → Accepted |
A reasonable suggestion; not for 1.3, however, because we have hit feature freeze, and this would be a new feature.
Out of interest, the BaseValidator (on which the min/max value validators is based) already does this; but the min/max validation strings don't exploit the capability.
comment:3 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:6 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 10 years ago
Cc: | added |
---|---|
Version: | 1.2 → master |
I have looked into this and there are some changes from when the ticket was filed.
In DateTimeField
in db/models/fields/__init__.py
default_error_messages = { 'invalid': _("'%(value)s' value has an invalid format. It must be in " "YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."), 'invalid_date': _("'%(value)s' value has the correct format " "(YYYY-MM-DD) but it is an invalid date."), 'invalid_datetime': _("'%(value)s' value has the correct format " "(YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) " "but it is an invalid date/time."), }
But in DateTimeField
in django/forms/fields.py
default_error_messages = { 'invalid': _('Enter a valid date/time.'), }
There have been a lot of changes in the source code from the time it was filed. So it seems that this is changed in db/models/fields/__init__.py
which is the place where Steve mentions he found the problem.
But the component in the the ticket says Forms
, in which the problem still exists as the errors from the forms still are not at all good if they just say Enter a valid date/time.
I am not sure that how the forms got there errors earlier, probably from models itself, but atleast now it is not.
- In many of the form fields, the error messages are much better and self-explanatory but the above and in many places like
DateField
,TimeField
,DateTimeField
,URLField
and maybe some more, I think we can make those better just like in thedb/models/fields/__init__.py
. - I think we should make
default_error_messages
in forms and models similar. Inmodels
, every error message contains the faultyvalue
but informs
, it does not. The description says to pass name of the field, but I think passing the faulty value just as in case of models would be much better.
So what I think that atleast we should improve errors in the form fields that I listed out above and other similar ones and also we can decide on the second point, i.e. whether we should pass the value onto the errors in the all form errors as is done in few so as to make it similar to that in models.
comment:9 by , 10 years ago
As for forms, I don't think we should change the error messages, as the field values and names are often obvious when the form is redisplayed with errors.
It would be nice if we could narrow the use cases targeted by this ticket (which is not forms IMHO).
comment:10 by , 10 years ago
The error messages in forms.fields in Date and Time related field do not even mention what is the correct format.
Probably the errors earlier were gathered from models.fields, but now if date is wrong, we get only 'Enter a valid date/time.'
I think we should atleast update those messages to the previous state where they tell what is the correct format, i.e. specifying YYYY-MM-DD HH:MM[:ss[.uuuuuu]]
for DateTimeField
and other related or similar ones or else if someone enters wrong in forms, we only get: Enter valid date/time.
comment:12 by , 10 years ago
IMO, I think what was asked by Steve, i.e. to include name
is no more required as default_error_messages
in db/models/field/__init__.py
have changed and they are good.
Though the new form errors now are even less better than previous ones mostly in date/time or url fields. So I think we should close this ticket and open a new one for the improvements in those messages with what I suggested.
If we agree to this, then I would request one of the admins to close this so I'll open a new one with the suggested.
Thanks.
comment:13 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I agree the original issue appears to be fixed.
Regarding forms, I think it's better if the developer includes help_text
or a custom error message if they wants to include the accepted formats. Field.input_formats
is something like [u'%Y-%m-%d', u'%m/%d/%Y', u'%m/%d/%y']
which most end users won't know how to interpret.
Sorry, the error is in: