Code

Opened 7 years ago

Closed 7 years ago

#5329 closed (wontfix)

URLField initial value, fails on validation

Reported by: danielrubio Owned by: adrian
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: UI/UX:

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.


Attachments (0)

Change History (1)

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.