Commits

Honz...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed dbe510d

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

  • Participants
  • Parent commits 32b460b
  • Branches soc2009/model-validation

Comments (0)

Files changed (5)

File django/forms/fields.py

         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."
             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.

File tests/regressiontests/forms/error_messages.py

 # 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):
 
 >>> 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):
 
 >>> 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):

File tests/regressiontests/forms/fields.py

     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')

File tests/regressiontests/forms/localflavor/ar.py

 >>> 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):
 ...
 >>> 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):
 ...
 >>> 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):
 ...
 >>> 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):
 ...

File tests/regressiontests/forms/localflavor/is_.py

 >>> 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):
 ...
 >>> 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):
 ...