Opened 11 years ago

Closed 11 years ago

#5329 closed (wontfix)

URLField initial value, fails on validation

Reported by: danielrubio Owned by: Adrian Holovaty
Component: Forms Version: master
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


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, 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

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 Changed 11 years ago by Chris Beaven

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