URLField with min_length or max_length reports wrong length in validation messages
|Reported by:||Fraser Nevett||Owned by:||ANUBHAV JOSHI|
|Cc:||me@…||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Here's a simple example to illustrate the problem:
>>> from django.forms import URLField >>> URLField(max_length=5).clean('example.com') Traceback (most recent call last): File "<console>", line 1, in <module> File "django/forms/fields.py", line 153, in clean self.run_validators(value) File "django/forms/fields.py", line 142, in run_validators raise ValidationError(errors) ValidationError: [u'Ensure this value has at most 5 characters (it has 19).']
The entered value (example.com) is only 11 characters long, but the validation message says that it is 19. This obviously has the potential to cause confusion to the user.
The value 19 comes from the fact that length check is performed after the value has been sanitised to http://example.com/.
A possible solution would be to add the sanitised value to the validation message so that the user can better understand to what value the length restriction actually applies. For example:
Ensure this value has at most 5 characters ("http://example.com/" has 19).
Note that the same problem occurs with
min_length as with
Change History (15)
comment:4 Changed 5 years ago by
|Patch needs improvement:||set|
|Status:||closed → reopened|
comment:8 Changed 3 years ago by
|Owner:||changed from version2beta to anonymous|
|Status:||new → assigned|
comment:12 Changed 3 years ago by
|Owner:||changed from Stewart Perrygrove to ANUBHAV JOSHI|
|Patch needs improvement:||unset|