Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #3038 -- newforms: RegexField no longer validates empty input f…

…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...
commit 2e4ff8ee0c0319a5c67c3fc4c4430e56562dfa53 1 parent d1757da
@adrianholovaty adrianholovaty authored
Showing with 19 additions and 3 deletions.
  1. +1 −1  django/newforms/fields.py
  2. +18 −2 tests/regressiontests/forms/tests.py
View
2  django/newforms/fields.py
@@ -170,7 +170,7 @@ def clean(self, value):
Field.clean(self, value)
if value in EMPTY_VALUES: value = u''
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)
return value
View
20 tests/regressiontests/forms/tests.py
@@ -484,13 +484,13 @@
u'hello'
>>> f.clean(None)
u''
+>>> f.clean('')
+u''
>>> f.clean([1, 2, 3])
u'[1, 2, 3]'
CharField accepts an optional max_length parameter:
>>> f = CharField(max_length=10, required=False)
->>> f.clean('')
-u''
>>> f.clean('12345')
u'12345'
>>> f.clean('1234567890')
@@ -700,6 +700,22 @@
Traceback (most recent call last):
...
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:
>>> f = RegexField(re.compile('^\d[A-F]\d$'))
Please sign in to comment.
Something went wrong with that request. Please try again.