Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Added max_length and min_length optional arguments to Regex…

…Field, 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
  • Loading branch information...
commit b5cd7d469d580133bd135b2d0f8ad41d342f6e07 1 parent d0fcef9
Adrian Holovaty authored December 26, 2006
16  django/newforms/fields.py
@@ -213,7 +213,8 @@ def clean(self, value):
213 213
         raise ValidationError(gettext(u'Enter a valid date/time.'))
214 214
 
215 215
 class RegexField(Field):
216  
-    def __init__(self, regex, error_message=None, required=True, widget=None, label=None):
  216
+    def __init__(self, regex, max_length=None, min_length=None, error_message=None,
  217
+            required=True, widget=None, label=None):
217 218
         """
218 219
         regex can be either a string or a compiled regular expression object.
219 220
         error_message is an optional error message to use, if
@@ -223,6 +224,7 @@ def __init__(self, regex, error_message=None, required=True, widget=None, label=
223 224
         if isinstance(regex, basestring):
224 225
             regex = re.compile(regex)
225 226
         self.regex = regex
  227
+        self.max_length, self.min_length = max_length, min_length
226 228
         self.error_message = error_message or gettext(u'Enter a valid value.')
227 229
 
228 230
     def clean(self, value):
@@ -235,6 +237,10 @@ def clean(self, value):
235 237
         value = smart_unicode(value)
236 238
         if not self.required and value == u'':
237 239
             return value
  240
+        if self.max_length is not None and len(value) > self.max_length:
  241
+            raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
  242
+        if self.min_length is not None and len(value) < self.min_length:
  243
+            raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length)
238 244
         if not self.regex.search(value):
239 245
             raise ValidationError(self.error_message)
240 246
         return value
@@ -245,8 +251,8 @@ def clean(self, value):
245 251
     r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain
246 252
 
247 253
 class EmailField(RegexField):
248  
-    def __init__(self, required=True, widget=None, label=None):
249  
-        RegexField.__init__(self, email_re, gettext(u'Enter a valid e-mail address.'), required, widget, label)
  254
+    def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None):
  255
+        RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label)
250 256
 
251 257
 url_re = re.compile(
252 258
     r'^https?://' # http:// or https://
@@ -262,9 +268,9 @@ def __init__(self, required=True, widget=None, label=None):
262 268
     URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
263 269
 
264 270
 class URLField(RegexField):
265  
-    def __init__(self, required=True, verify_exists=False, widget=None, label=None,
  271
+    def __init__(self, max_length=None, min_length=None, required=True, verify_exists=False, widget=None, label=None,
266 272
             validator_user_agent=URL_VALIDATOR_USER_AGENT):
267  
-        RegexField.__init__(self, url_re, gettext(u'Enter a valid URL.'), required, widget, label)
  273
+        RegexField.__init__(self, url_re, max_length, min_length, gettext(u'Enter a valid URL.'), required, widget, label)
268 274
         self.verify_exists = verify_exists
269 275
         self.user_agent = validator_user_agent
270 276
 
51  tests/regressiontests/forms/tests.py
@@ -1098,7 +1098,7 @@
1098 1098
 ValidationError: [u'Enter a valid value.']
1099 1099
 
1100 1100
 RegexField takes an optional error_message argument:
1101  
->>> f = RegexField('^\d\d\d\d$', 'Enter a four-digit number.')
  1101
+>>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.')
1102 1102
 >>> f.clean('1234')
1103 1103
 u'1234'
1104 1104
 >>> f.clean('123')
@@ -1110,6 +1110,29 @@
1110 1110
 ...
1111 1111
 ValidationError: [u'Enter a four-digit number.']
1112 1112
 
  1113
+RegexField also access min_length and max_length parameters, for convenience.
  1114
+>>> f = RegexField('^\d+$', min_length=5, max_length=10)
  1115
+>>> f.clean('123')
  1116
+Traceback (most recent call last):
  1117
+...
  1118
+ValidationError: [u'Ensure this value has at least 5 characters.']
  1119
+>>> f.clean('abc')
  1120
+Traceback (most recent call last):
  1121
+...
  1122
+ValidationError: [u'Ensure this value has at least 5 characters.']
  1123
+>>> f.clean('12345')
  1124
+u'12345'
  1125
+>>> f.clean('1234567890')
  1126
+u'1234567890'
  1127
+>>> f.clean('12345678901')
  1128
+Traceback (most recent call last):
  1129
+...
  1130
+ValidationError: [u'Ensure this value has at most 10 characters.']
  1131
+>>> f.clean('12345a')
  1132
+Traceback (most recent call last):
  1133
+...
  1134
+ValidationError: [u'Enter a valid value.']
  1135
+
1113 1136
 # EmailField ##################################################################
1114 1137
 
1115 1138
 >>> f = EmailField()
@@ -1156,6 +1179,19 @@
1156 1179
 ...
1157 1180
 ValidationError: [u'Enter a valid e-mail address.']
1158 1181
 
  1182
+EmailField also access min_length and max_length parameters, for convenience.
  1183
+>>> f = EmailField(min_length=10, max_length=15)
  1184
+>>> f.clean('a@foo.com')
  1185
+Traceback (most recent call last):
  1186
+...
  1187
+ValidationError: [u'Ensure this value has at least 10 characters.']
  1188
+>>> f.clean('alf@foo.com')
  1189
+u'alf@foo.com'
  1190
+>>> f.clean('alf123456788@foo.com')
  1191
+Traceback (most recent call last):
  1192
+...
  1193
+ValidationError: [u'Ensure this value has at most 15 characters.']
  1194
+
1159 1195
 # URLField ##################################################################
1160 1196
 
1161 1197
 >>> f = URLField()
@@ -1248,6 +1284,19 @@
1248 1284
 ...
1249 1285
 ValidationError: [u'This URL appears to be a broken link.']
1250 1286
 
  1287
+EmailField also access min_length and max_length parameters, for convenience.
  1288
+>>> f = URLField(min_length=15, max_length=20)
  1289
+>>> f.clean('http://f.com')
  1290
+Traceback (most recent call last):
  1291
+...
  1292
+ValidationError: [u'Ensure this value has at least 15 characters.']
  1293
+>>> f.clean('http://example.com')
  1294
+u'http://example.com'
  1295
+>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
  1296
+Traceback (most recent call last):
  1297
+...
  1298
+ValidationError: [u'Ensure this value has at most 20 characters.']
  1299
+
1251 1300
 # BooleanField ################################################################
1252 1301
 
1253 1302
 >>> f = BooleanField()

0 notes on commit b5cd7d4

Please sign in to comment.
Something went wrong with that request. Please try again.