Skip to content

Commit

Permalink
Fixed #3038 -- newforms: RegexField no longer validates empty input f…
Browse files Browse the repository at this point in the history
…or required=False. Thanks for reporting, Thomas Steinacher

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4111 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Nov 27, 2006
1 parent d1757da commit 2e4ff8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django/newforms/fields.py
Expand Up @@ -170,7 +170,7 @@ def clean(self, value):
Field.clean(self, value) Field.clean(self, value)
if value in EMPTY_VALUES: value = u'' if value in EMPTY_VALUES: value = u''
value = smart_unicode(value) value = smart_unicode(value)
if not self.regex.search(value): if (value or self.required) and not self.regex.search(value):
raise ValidationError(self.error_message) raise ValidationError(self.error_message)
return value return value


Expand Down
20 changes: 18 additions & 2 deletions tests/regressiontests/forms/tests.py
Expand Up @@ -484,13 +484,13 @@
u'hello' u'hello'
>>> f.clean(None) >>> f.clean(None)
u'' u''
>>> f.clean('')
u''
>>> f.clean([1, 2, 3]) >>> f.clean([1, 2, 3])
u'[1, 2, 3]' u'[1, 2, 3]'
CharField accepts an optional max_length parameter: CharField accepts an optional max_length parameter:
>>> f = CharField(max_length=10, required=False) >>> f = CharField(max_length=10, required=False)
>>> f.clean('')
u''
>>> f.clean('12345') >>> f.clean('12345')
u'12345' u'12345'
>>> f.clean('1234567890') >>> f.clean('1234567890')
Expand Down Expand Up @@ -700,6 +700,22 @@
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Enter a valid value.'] ValidationError: [u'Enter a valid value.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = RegexField('^\d[A-F]\d$', required=False)
>>> f.clean('2A2')
u'2A2'
>>> f.clean('3F3')
u'3F3'
>>> f.clean('3G3')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid value.']
>>> f.clean('')
u''
Alternatively, RegexField can take a compiled regular expression: Alternatively, RegexField can take a compiled regular expression:
>>> f = RegexField(re.compile('^\d[A-F]\d$')) >>> f = RegexField(re.compile('^\d[A-F]\d$'))
Expand Down

0 comments on commit 2e4ff8e

Please sign in to comment.