Opened 17 years ago

Closed 17 years ago

#5329 closed (wontfix)

URLField initial value, fails on validation

Reported by: danielrubio Owned by: Adrian Holovaty
Component: Forms Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Caught one more bug on new forms, this one is related to URLField.

If your form has the following declaration:
site = forms.URLField(required=False,initial='http://')

'http://' is placed by default on the form, however, validation fails even though 'required=False' is also used. So the user is forced to either introduce a valid URL or delete the 'http://' snippet from the box.

The reason behind the bug is that the 'initial' parameter is never taken into account in field.py, under django/newforms
To fix, its necessary to modify the clean method on the RegEx field class (Line: 316), to read like follows:

def clean(self, value):

"""
Validates that the input matches the regular expression. Returns a
Unicode object.
"""
value = super(RegexField, self).clean(value)
if value == u:

return value

if value == self.initial:

return None

if not self.regex.search(value):

raise ValidationError(self.error_message)

return value

The if value == self.initial return None, will fix the validation error. NOTE: The fix is in the RegEx field, on account the URLField delegates validation up to this class.


Change History (1)

comment:1 by Chris Beaven, 17 years ago

Resolution: wontfix
Status: newclosed

An initial value doesn't always equal a blank value though. The solution is to sub-class the field and add your own cleaning in if you need it.

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