Ticket #4861: 4861-regexfield.patch

File 4861-regexfield.patch, 3.0 KB (added by Collin Grady <cgrady@…>, 12 years ago)
  • django/newforms/fields.py

     
    295295                continue
    296296        raise ValidationError(ugettext(u'Enter a valid date/time.'))
    297297
    298 class RegexField(Field):
    299     def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
     298class RegexField(CharField):
     299    def __init__(self, regex, error_message=None, *args, **kwargs):
    300300        """
    301301        regex can be either a string or a compiled regular expression object.
    302302        error_message is an optional error message to use, if
     
    306306        if isinstance(regex, basestring):
    307307            regex = re.compile(regex)
    308308        self.regex = regex
    309         self.max_length, self.min_length = max_length, min_length
    310309        self.error_message = error_message or ugettext(u'Enter a valid value.')
    311310
    312311    def clean(self, value):
     
    314313        Validates that the input matches the regular expression. Returns a
    315314        Unicode object.
    316315        """
    317         super(RegexField, self).clean(value)
    318         if value in EMPTY_VALUES:
    319             value = u''
    320         value = smart_unicode(value)
     316        value = super(RegexField, self).clean(value)
    321317        if value == u'':
    322318            return value
    323         if self.max_length is not None and len(value) > self.max_length:
    324             raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
    325         if self.min_length is not None and len(value) < self.min_length:
    326             raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
    327319        if not self.regex.search(value):
    328320            raise ValidationError(self.error_message)
    329321        return value
     
    334326    r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
    335327
    336328class EmailField(RegexField):
    337     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    338         RegexField.__init__(self, email_re, max_length, min_length,
    339             ugettext(u'Enter a valid e-mail address.'), *args, **kwargs)
     329    def __init__(self, *args, **kwargs):
     330        super(EmailField, self).__init__(self, email_re, ugettext(u'Enter a valid e-mail address.'),
     331            *args, **kwargs)
    340332
    341333url_re = re.compile(
    342334    r'^https?://' # http:// or https://
     
    352344    URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
    353345
    354346class URLField(RegexField):
    355     def __init__(self, max_length=None, min_length=None, verify_exists=False,
     347    def __init__(self, verify_exists=False,
    356348            validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
    357         super(URLField, self).__init__(url_re, max_length, min_length, ugettext(u'Enter a valid URL.'), *args, **kwargs)
     349        super(URLField, self).__init__(url_re, ugettext(u'Enter a valid URL.'), *args, **kwargs)
    358350        self.verify_exists = verify_exists
    359351        self.user_agent = validator_user_agent
    360352
Back to Top