URLField initial value, fails on validation
|Reported by:||danielrubio||Owned by:||adrian|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||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 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
value = super(RegexField, self).clean(value)
if value == u:
if value == self.initial:
if not self.regex.search(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.