1
0
mirror of https://github.com/django/django.git synced 2025-07-07 03:09:22 +00:00

[soc2009/model-validation] use RegexValidator in RegexField

declarative definition prepared for RegexValidator children

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11455 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-08-15 13:33:57 +00:00
parent 84c9cf16d3
commit 5ebafe3fb9
2 changed files with 13 additions and 13 deletions

View File

@ -23,6 +23,14 @@ url_re = re.compile(
r'(?:/?|/\S+)$', re.IGNORECASE)
class RegexValidator(object):
regex = ''
def __init__(self, regex=None):
if regex is not None:
self.regex = regex
if isinstance(self.regex, basestring):
self.regex = re.compile(regex)
def __call__(self, value):
"""
Validates that the input matches the regular expression.
@ -31,8 +39,10 @@ class RegexValidator(object):
raise ValidationError(_(u'Enter a valid value.'), code='invalid')
class URLValidator(RegexValidator):
def __init__(self, regex=url_re, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
self.regex = regex
regex = url_re
def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
super(URLValidator, self).__init__()
self.verify_exists = verify_exists
self.user_agent = validator_user_agent

View File

@ -439,17 +439,7 @@ class RegexField(CharField):
if isinstance(regex, basestring):
regex = re.compile(regex)
self.regex = regex
def validate(self, value):
"""
Validates that the input matches the regular expression. Returns a
Unicode object.
"""
super(RegexField, self).validate(value)
if value == u'':
return value
if not self.regex.search(value):
raise ValidationError(self.error_messages['invalid'])
self.validators.append(validators.RegexValidator(regex=regex))
class EmailField(CharField):
default_error_messages = {