mirror of https://github.com/django/django.git
newforms: Added max_length and min_length optional arguments to RegexField, EmailField and URLField. Note that the argument order for those three fields has changed
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d0fcef9db0
commit
b5cd7d469d
|
@ -213,7 +213,8 @@ class DateTimeField(Field):
|
|||
raise ValidationError(gettext(u'Enter a valid date/time.'))
|
||||
|
||||
class RegexField(Field):
|
||||
def __init__(self, regex, error_message=None, required=True, widget=None, label=None):
|
||||
def __init__(self, regex, max_length=None, min_length=None, error_message=None,
|
||||
required=True, widget=None, label=None):
|
||||
"""
|
||||
regex can be either a string or a compiled regular expression object.
|
||||
error_message is an optional error message to use, if
|
||||
|
@ -223,6 +224,7 @@ class RegexField(Field):
|
|||
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 gettext(u'Enter a valid value.')
|
||||
|
||||
def clean(self, value):
|
||||
|
@ -235,6 +237,10 @@ class RegexField(Field):
|
|||
value = smart_unicode(value)
|
||||
if not self.required and value == u'':
|
||||
return value
|
||||
if self.max_length is not None and len(value) > self.max_length:
|
||||
raise ValidationError(gettext(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(gettext(u'Ensure this value has at least %d characters.') % self.min_length)
|
||||
if not self.regex.search(value):
|
||||
raise ValidationError(self.error_message)
|
||||
return value
|
||||
|
@ -245,8 +251,8 @@ email_re = re.compile(
|
|||
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
|
||||
|
||||
class EmailField(RegexField):
|
||||
def __init__(self, required=True, widget=None, label=None):
|
||||
RegexField.__init__(self, email_re, gettext(u'Enter a valid e-mail address.'), required, widget, label)
|
||||
def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None):
|
||||
RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label)
|
||||
|
||||
url_re = re.compile(
|
||||
r'^https?://' # http:// or https://
|
||||
|
@ -262,9 +268,9 @@ except ImportError:
|
|||
URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
|
||||
|
||||
class URLField(RegexField):
|
||||
def __init__(self, required=True, verify_exists=False, widget=None, label=None,
|
||||
def __init__(self, max_length=None, min_length=None, required=True, verify_exists=False, widget=None, label=None,
|
||||
validator_user_agent=URL_VALIDATOR_USER_AGENT):
|
||||
RegexField.__init__(self, url_re, gettext(u'Enter a valid URL.'), required, widget, label)
|
||||
RegexField.__init__(self, url_re, max_length, min_length, gettext(u'Enter a valid URL.'), required, widget, label)
|
||||
self.verify_exists = verify_exists
|
||||
self.user_agent = validator_user_agent
|
||||
|
||||
|
|
|
@ -1098,7 +1098,7 @@ Traceback (most recent call last):
|
|||
ValidationError: [u'Enter a valid value.']
|
||||
|
||||
RegexField takes an optional error_message argument:
|
||||
>>> f = RegexField('^\d\d\d\d$', 'Enter a four-digit number.')
|
||||
>>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.')
|
||||
>>> f.clean('1234')
|
||||
u'1234'
|
||||
>>> f.clean('123')
|
||||
|
@ -1110,6 +1110,29 @@ Traceback (most recent call last):
|
|||
...
|
||||
ValidationError: [u'Enter a four-digit number.']
|
||||
|
||||
RegexField also access min_length and max_length parameters, for convenience.
|
||||
>>> f = RegexField('^\d+$', min_length=5, max_length=10)
|
||||
>>> f.clean('123')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at least 5 characters.']
|
||||
>>> f.clean('abc')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at least 5 characters.']
|
||||
>>> f.clean('12345')
|
||||
u'12345'
|
||||
>>> f.clean('1234567890')
|
||||
u'1234567890'
|
||||
>>> f.clean('12345678901')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at most 10 characters.']
|
||||
>>> f.clean('12345a')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Enter a valid value.']
|
||||
|
||||
# EmailField ##################################################################
|
||||
|
||||
>>> f = EmailField()
|
||||
|
@ -1156,6 +1179,19 @@ Traceback (most recent call last):
|
|||
...
|
||||
ValidationError: [u'Enter a valid e-mail address.']
|
||||
|
||||
EmailField also access min_length and max_length parameters, for convenience.
|
||||
>>> f = EmailField(min_length=10, max_length=15)
|
||||
>>> f.clean('a@foo.com')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at least 10 characters.']
|
||||
>>> f.clean('alf@foo.com')
|
||||
u'alf@foo.com'
|
||||
>>> f.clean('alf123456788@foo.com')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at most 15 characters.']
|
||||
|
||||
# URLField ##################################################################
|
||||
|
||||
>>> f = URLField()
|
||||
|
@ -1248,6 +1284,19 @@ Traceback (most recent call last):
|
|||
...
|
||||
ValidationError: [u'This URL appears to be a broken link.']
|
||||
|
||||
EmailField also access min_length and max_length parameters, for convenience.
|
||||
>>> f = URLField(min_length=15, max_length=20)
|
||||
>>> f.clean('http://f.com')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at least 15 characters.']
|
||||
>>> f.clean('http://example.com')
|
||||
u'http://example.com'
|
||||
>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'Ensure this value has at most 20 characters.']
|
||||
|
||||
# BooleanField ################################################################
|
||||
|
||||
>>> f = BooleanField()
|
||||
|
|
Loading…
Reference in New Issue