Opened 16 years ago

Closed 16 years ago

#6717 closed (duplicate)

newforms URL validation rejects leading/trailing space

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

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 Antti Kaihola 16 years ago.
patch: newforms.URLField ignores leading/trailing space
6717-url-strip-v2-with-tests.diff (1.1 KB ) - added by Antti Kaihola 16 years ago.
revised patch: handles None correctly, includes unit tests

Download all attachments as: .zip

Change History (4)

by Antti Kaihola, 16 years ago

Attachment: 6717-url-strip.diff added

patch: newforms.URLField ignores leading/trailing space

by Antti Kaihola, 16 years ago

revised patch: handles None correctly, includes unit tests

comment:1 by Ramiro Morales, 16 years ago

See also #6362 for a more general proposal.

comment:2 by Jacob, 16 years ago

Resolution: duplicate
Status: newclosed

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

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