From b5cd7d469d580133bd135b2d0f8ad41d342f6e07 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 26 Dec 2006 23:46:10 +0000 Subject: [PATCH] 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 --- django/newforms/fields.py | 16 ++++++--- tests/regressiontests/forms/tests.py | 51 +++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/django/newforms/fields.py b/django/newforms/fields.py index ec3b7fc3189..5c6d46ddac0 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -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 diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index f9fd040bd2f..50bc7a75954 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -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()