Ticket #13511: 13511-regexvalidator.diff

File 13511-regexvalidator.diff, 1.6 KB (added by David Fischer, 14 years ago)

switched regex parameter to be required

  • django/core/validators.py

     
    1616    URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
    1717
    1818class RegexValidator(object):
    19     regex = ''
    2019    message = _(u'Enter a valid value.')
    2120    code = 'invalid'
    2221
    23     def __init__(self, regex=None, message=None, code=None):
    24         if regex is not None:
    25             self.regex = regex
     22    def __init__(self, regex, message=None, code=None):
    2623        if message is not None:
    2724            self.message = message
    2825        if code is not None:
    2926            self.code = code
    3027
    31         if isinstance(self.regex, basestring):
     28        if isinstance(regex, basestring):
    3229            self.regex = re.compile(regex)
     30        else:
     31            # regex is already compiled
     32            self.regex = regex
    3333
    3434    def __call__(self, value):
    3535        """
     
    3939            raise ValidationError(self.message, code=self.code)
    4040
    4141class URLValidator(RegexValidator):
    42     regex = re.compile(
     42    URL_PATTERN = re.compile(
    4343        r'^https?://' # http:// or https://
    4444        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
    4545        r'localhost|' #localhost...
     
    4848        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
    4949
    5050    def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
    51         super(URLValidator, self).__init__()
     51        super(URLValidator, self).__init__(self.URL_PATTERN)
    5252        self.verify_exists = verify_exists
    5353        self.user_agent = validator_user_agent
    5454
Back to Top