Opened 18 years ago
Closed 18 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)
Change History (4)
by , 18 years ago
| Attachment: | 6717-url-strip.diff added |
|---|
by , 18 years ago
| Attachment: | 6717-url-strip-v2-with-tests.diff added |
|---|
revised patch: handles None correctly, includes unit tests
comment:2 by , 18 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Yeah, #6362 is the bigger issue here. It wouldn't make sense to treat URLs differently from other field types -- consistency, consistency, consistency.
patch: newforms.URLField ignores leading/trailing space