Permalink
Browse files

[soc2009/model-validation] Migrated CharField to use validators

  • Loading branch information...
1 parent 22795e1 commit c3e94faa23dba7c49648bc19c643abd54da4f205 @HonzaKral HonzaKral committed Dec 29, 2009
View
@@ -170,38 +170,20 @@ def __deepcopy__(self, memo):
return result
class CharField(Field):
- default_error_messages = {
- 'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
- 'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
- }
-
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length
super(CharField, self).__init__(*args, **kwargs)
- # TODO: use this as soon as you make regex validator and use it in RegexField
- #if min_length is not None:
- # self.validators.append(validators.MinLengthValidator(min_length))
- #if max_length is not None:
- # self.validators.append(validators.MaxLengthValidator(max_length))
+ if min_length is not None:
+ self.validators.append(validators.MinLengthValidator(min_length))
+ if max_length is not None:
+ self.validators.append(validators.MaxLengthValidator(max_length))
def to_python(self, value):
"Returns a Unicode object."
if value in validators.EMPTY_VALUES:
return u''
return smart_unicode(value)
- def validate(self, value):
- "Validates max_length and min_length."
- super(CharField, self).validate(value)
- if value in validators.EMPTY_VALUES:
- # non-required field, no need for further validation
- return
- value_length = len(value)
- if self.max_length is not None and value_length > self.max_length:
- raise ValidationError(self.error_messages['max_length'] % {'max': self.max_length, 'length': value_length})
- if self.min_length is not None and value_length < self.min_length:
- raise ValidationError(self.error_messages['min_length'] % {'min': self.min_length, 'length': value_length})
-
def widget_attrs(self, widget):
if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
# The HTML attribute is maxlength, not max_length.
@@ -6,8 +6,8 @@
# CharField ###################################################################
>>> e = {'required': 'REQUIRED'}
->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
+>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
+>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
>>> f = CharField(min_length=5, max_length=10, error_messages=e)
>>> f.clean('')
Traceback (most recent call last):
@@ -156,8 +156,8 @@
>>> e = {'required': 'REQUIRED'}
>>> e['invalid'] = 'INVALID'
->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
+>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
+>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
>>> f = RegexField(r'^\d+$', min_length=5, max_length=10, error_messages=e)
>>> f.clean('')
Traceback (most recent call last):
@@ -180,8 +180,8 @@
>>> e = {'required': 'REQUIRED'}
>>> e['invalid'] = 'INVALID'
->>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
->>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
+>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
+>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
>>> f = EmailField(min_length=8, max_length=10, error_messages=e)
>>> f.clean('')
Traceback (most recent call last):
@@ -386,7 +386,7 @@ def test_regexfield_30(self):
def test_regexfield_31(self):
f = RegexField('^\d+$', min_length=5, max_length=10)
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123')
- self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, 'abc')
+ self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc')
self.assertEqual(u'12345', f.clean('12345'))
self.assertEqual(u'1234567890', f.clean('1234567890'))
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901')
@@ -28,7 +28,7 @@
>>> f.clean('C1064AABB')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
>>> f.clean('C1064AA')
Traceback (most recent call last):
...
@@ -44,7 +44,7 @@
>>> f.clean('500')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 4 characters (it has 3).']
+ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
>>> f.clean('5PPP')
Traceback (most recent call last):
...
@@ -78,7 +78,7 @@
>>> f.clean('C1064AABB')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
>>> f.clean('C1064AA')
Traceback (most recent call last):
...
@@ -94,7 +94,7 @@
>>> f.clean('500')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 4 characters (it has 3).']
+ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
>>> f.clean('5PPP')
Traceback (most recent call last):
...
@@ -15,11 +15,11 @@
>>> f.clean('230880343')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
+ValidationError: [u'Ensure this value has at least 10 characters (it has 9).', u'Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.']
>>> f.clean('230880343234')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 11 characters (it has 12).']
+ValidationError: [u'Ensure this value has at most 11 characters (it has 12).', u'Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.']
>>> f.clean('abcdefghijk')
Traceback (most recent call last):
...
@@ -61,18 +61,18 @@
>>> f.clean('123456')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 7 characters (it has 6).']
+ValidationError: [u'Ensure this value has at least 7 characters (it has 6).', u'Enter a valid value.']
>>> f.clean('123456555')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.']
>>> f.clean('abcdefg')
Traceback (most recent call last):
ValidationError: [u'Enter a valid value.']
>>> f.clean(' 1234567 ')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.']
>>> f.clean(' 12367 ')
Traceback (most recent call last):
...

0 comments on commit c3e94fa

Please sign in to comment.