Browse files

Fixed #4861 -- Removed some duplicated logic from the newforms RegexF…

…ield by

making it a subclass of CharField. Thanks, Collin Grady.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 0145e71 commit e4bcfc272c4ae6fa5c5b5ffa6fef06fa0cdfd4be @malcolmt malcolmt committed Jul 13, 2007
Showing with 3 additions and 11 deletions.
  1. +3 −11 django/newforms/
@@ -298,35 +298,27 @@ def clean(self, value):
raise ValidationError(ugettext(u'Enter a valid date/time.'))
-class RegexField(Field):
+class RegexField(CharField):
def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
regex can be either a string or a compiled regular expression object.
error_message is an optional error message to use, if
'Enter a valid value' is too generic for you.
- super(RegexField, self).__init__(*args, **kwargs)
+ super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
if isinstance(regex, basestring):
regex = re.compile(regex)
self.regex = regex
- self.max_length, self.min_length = max_length, min_length
self.error_message = error_message or ugettext(u'Enter a valid value.')
def clean(self, value):
Validates that the input matches the regular expression. Returns a
Unicode object.
- super(RegexField, self).clean(value)
- if value in EMPTY_VALUES:
- value = u''
- value = smart_unicode(value)
+ value = super(RegexField, self).clean(value)
if value == u'':
return value
- if self.max_length is not None and len(value) > self.max_length:
- raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
- if self.min_length is not None and len(value) < self.min_length:
- raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
if not
raise ValidationError(self.error_message)
return value

0 comments on commit e4bcfc2

Please sign in to comment.