Opened 5 years ago

Closed 2 years ago

#16617 closed Bug (fixed)

URLField with min_length or max_length reports wrong length in validation messages

Reported by: Fraser Nevett Owned by: ANUBHAV JOSHI
Component: Forms Version: master
Severity: Normal Keywords:
Cc: me@… 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

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 max_length.

Attachments (1)

length-validators.diff (1.5 KB) - added by version2beta 5 years ago.
Patch shows string rather than length for min|max length

Download all attachments as: .zip

Change History (15)

comment:1 Changed 5 years ago by Bernhard Essl

Cc: me@… added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 5 years ago by version2beta

Owner: changed from nobody to version2beta
Status: newassigned

Changed 5 years ago by version2beta

Attachment: length-validators.diff added

Patch shows string rather than length for min|max length

comment:3 Changed 5 years ago by version2beta

Has patch: set
Resolution: fixed
Status: assignedclosed

Patch attached for django/core/validators.py that allows 'value' to remain as its original type, rather than being converted to an integer length. This allows the message to contain the string, or in the case of MaxLengthValidator, the first limit_value chars of the string.

comment:4 Changed 5 years ago by Fraser Nevett

Needs tests: set
Patch needs improvement: set
Resolution: fixed
Status: closedreopened

Reopening ticket as the patch hasn't actually been committed, so the ticket is not yet fixed.

I'm also not sure that the patch is likely to be accepted when reviewed by a core dev as it's not immediately obvious how it resolves the problem and there are no test cases to verify it is behaving correctly.

comment:5 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:6 Changed 3 years ago by Stewart Perrygrove

Pull request: https://github.com/django/django/pull/2261

Makes value available to BaseValidator types for use in the MinLengthValidator/MaxLengthValidator message.

Last edited 3 years ago by Stewart Perrygrove (previous) (diff)

comment:7 Changed 3 years ago by Tim Graham

Including value in the params dict looks good, but I wonder if it might be better to leave the default error messages as is and either offer an option to easily switch the default error message to include the value or just let people override the message as they see fit. In the case of really long values, it could be undesirable to have the value repeated in the error message. Any thoughts?

comment:8 Changed 3 years ago by anonymous

Owner: changed from version2beta to anonymous
Status: newassigned

comment:9 Changed 3 years ago by Stewart Perrygrove

Owner: changed from anonymous to Stewart Perrygrove

comment:10 Changed 2 years ago by ANUBHAV JOSHI

@sperrygrove
You interested in finishing this?

comment:11 Changed 2 years ago by Stewart Perrygrove

@anubhav9042 - please take it if you'd like to work on it.

comment:12 Changed 2 years ago by ANUBHAV JOSHI

Needs tests: unset
Owner: changed from Stewart Perrygrove to ANUBHAV JOSHI
Patch needs improvement: unset

comment:13 Changed 2 years ago by johmik

Triage Stage: AcceptedReady for checkin

comment:14 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 5cdb8f8c1ea7161ca9e3b702ec7cdd7e3cd0999a:

Fixed #16617 -- Added 'value' to BaseValidator params.

Also allowed overriding the default messages in subclasses of BaseValidator.

Thanks sperrygrove for initial patch.

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