Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ield by

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


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5684 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e4bcfc272c4ae6fa5c5b5ffa6fef06fa0cdfd4be 1 parent 0145e71
Malcolm Tredinnick authored July 13, 2007

Showing 1 changed file with 3 additions and 11 deletions. Show diff stats Hide diff stats

  1. 14  django/newforms/fields.py
14  django/newforms/fields.py
@@ -298,18 +298,17 @@ def clean(self, value):
298 298
                 continue
299 299
         raise ValidationError(ugettext(u'Enter a valid date/time.'))
300 300
 
301  
-class RegexField(Field):
  301
+class RegexField(CharField):
302 302
     def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
303 303
         """
304 304
         regex can be either a string or a compiled regular expression object.
305 305
         error_message is an optional error message to use, if
306 306
         'Enter a valid value' is too generic for you.
307 307
         """
308  
-        super(RegexField, self).__init__(*args, **kwargs)
  308
+        super(RegexField, self).__init__(max_length, min_length, *args, **kwargs)
309 309
         if isinstance(regex, basestring):
310 310
             regex = re.compile(regex)
311 311
         self.regex = regex
312  
-        self.max_length, self.min_length = max_length, min_length
313 312
         self.error_message = error_message or ugettext(u'Enter a valid value.')
314 313
 
315 314
     def clean(self, value):
@@ -317,16 +316,9 @@ def clean(self, value):
317 316
         Validates that the input matches the regular expression. Returns a
318 317
         Unicode object.
319 318
         """
320  
-        super(RegexField, self).clean(value)
321  
-        if value in EMPTY_VALUES:
322  
-            value = u''
323  
-        value = smart_unicode(value)
  319
+        value = super(RegexField, self).clean(value)
324 320
         if value == u'':
325 321
             return value
326  
-        if self.max_length is not None and len(value) > self.max_length:
327  
-            raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
328  
-        if self.min_length is not None and len(value) < self.min_length:
329  
-            raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
330 322
         if not self.regex.search(value):
331 323
             raise ValidationError(self.error_message)
332 324
         return value

0 notes on commit e4bcfc2

Please sign in to comment.
Something went wrong with that request. Please try again.