Code

Opened 6 years ago

Closed 6 years ago

#6717 closed (duplicate)

newforms URL validation rejects leading/trailing space

Reported by: akaihola Owned by: nobody
Component: Forms Version: master
Severity: Keywords: url validation
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

URL fields raise a validation error if a valid URL has leading or trailing spaces. A string with only spaces also raises an error instead of being interpreted as empty when the field is not required.

>>> from django import newforms as forms
>>> u=forms.URLField(required=False)
>>> u.clean('www.djangoproject.com')
u'http://www.djangoproject.com'
>>> u.clean('  http://www.djangoproject.com/')
ValidationError: [u'Enter a valid URL.']
>>> u.clean('http://www.djangoproject.com/  ')
ValidationError: [u'Enter a valid URL.']
>>> u.clean('  ')
ValidationError: [u'Enter a valid URL.']

Django should first strip leading and trailing spaces from the URL before validating it.

Rationale:

  • browser address fields strip URLs
  • Python's urllib and urllib2 open URLs with leading/trailing spaces correctly
  • leading/trailing spaces and only-space strings are hard to spot for non-experienced web users
  • "Be liberal in what you accept, and conservative in what you send"

Attachments (2)

6717-url-strip.diff (456 bytes) - added by akaihola 6 years ago.
patch: newforms.URLField ignores leading/trailing space
6717-url-strip-v2-with-tests.diff (1.1 KB) - added by akaihola 6 years ago.
revised patch: handles None correctly, includes unit tests

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by akaihola

patch: newforms.URLField ignores leading/trailing space

Changed 6 years ago by akaihola

revised patch: handles None correctly, includes unit tests

comment:1 Changed 6 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

See also #6362 for a more general proposal.

comment:2 Changed 6 years ago by jacob

  • Resolution set to duplicate
  • Status changed from new to closed

Yeah, #6362 is the bigger issue here. It wouldn't make sense to treat URLs differently from other field types -- consistency, consistency, consistency.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.