Skip to content

Commit c3e94fa

Browse files
committed
[soc2009/model-validation] Migrated CharField to use validators
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@12018 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 22795e1 commit c3e94fa

File tree

5 files changed

+20
-38
lines changed

5 files changed

+20
-38
lines changed

django/forms/fields.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,38 +170,20 @@ def __deepcopy__(self, memo):
170170
return result
171171

172172
class CharField(Field):
173-
default_error_messages = {
174-
'max_length': _(u'Ensure this value has at most %(max)d characters (it has %(length)d).'),
175-
'min_length': _(u'Ensure this value has at least %(min)d characters (it has %(length)d).'),
176-
}
177-
178173
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
179174
self.max_length, self.min_length = max_length, min_length
180175
super(CharField, self).__init__(*args, **kwargs)
181-
# TODO: use this as soon as you make regex validator and use it in RegexField
182-
#if min_length is not None:
183-
# self.validators.append(validators.MinLengthValidator(min_length))
184-
#if max_length is not None:
185-
# self.validators.append(validators.MaxLengthValidator(max_length))
176+
if min_length is not None:
177+
self.validators.append(validators.MinLengthValidator(min_length))
178+
if max_length is not None:
179+
self.validators.append(validators.MaxLengthValidator(max_length))
186180

187181
def to_python(self, value):
188182
"Returns a Unicode object."
189183
if value in validators.EMPTY_VALUES:
190184
return u''
191185
return smart_unicode(value)
192186

193-
def validate(self, value):
194-
"Validates max_length and min_length."
195-
super(CharField, self).validate(value)
196-
if value in validators.EMPTY_VALUES:
197-
# non-required field, no need for further validation
198-
return
199-
value_length = len(value)
200-
if self.max_length is not None and value_length > self.max_length:
201-
raise ValidationError(self.error_messages['max_length'] % {'max': self.max_length, 'length': value_length})
202-
if self.min_length is not None and value_length < self.min_length:
203-
raise ValidationError(self.error_messages['min_length'] % {'min': self.min_length, 'length': value_length})
204-
205187
def widget_attrs(self, widget):
206188
if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
207189
# The HTML attribute is maxlength, not max_length.

tests/regressiontests/forms/error_messages.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# CharField ###################################################################
77
88
>>> e = {'required': 'REQUIRED'}
9-
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
10-
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
9+
>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
10+
>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
1111
>>> f = CharField(min_length=5, max_length=10, error_messages=e)
1212
>>> f.clean('')
1313
Traceback (most recent call last):
@@ -156,8 +156,8 @@
156156
157157
>>> e = {'required': 'REQUIRED'}
158158
>>> e['invalid'] = 'INVALID'
159-
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
160-
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
159+
>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
160+
>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
161161
>>> f = RegexField(r'^\d+$', min_length=5, max_length=10, error_messages=e)
162162
>>> f.clean('')
163163
Traceback (most recent call last):
@@ -180,8 +180,8 @@
180180
181181
>>> e = {'required': 'REQUIRED'}
182182
>>> e['invalid'] = 'INVALID'
183-
>>> e['min_length'] = 'LENGTH %(length)s, MIN LENGTH %(min)s'
184-
>>> e['max_length'] = 'LENGTH %(length)s, MAX LENGTH %(max)s'
183+
>>> e['min_length'] = 'LENGTH %(show_value)s, MIN LENGTH %(limit_value)s'
184+
>>> e['max_length'] = 'LENGTH %(show_value)s, MAX LENGTH %(limit_value)s'
185185
>>> f = EmailField(min_length=8, max_length=10, error_messages=e)
186186
>>> f.clean('')
187187
Traceback (most recent call last):

tests/regressiontests/forms/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def test_regexfield_30(self):
386386
def test_regexfield_31(self):
387387
f = RegexField('^\d+$', min_length=5, max_length=10)
388388
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, '123')
389-
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).']", f.clean, 'abc')
389+
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at least 5 characters (it has 3).', u'Enter a valid value.']", f.clean, 'abc')
390390
self.assertEqual(u'12345', f.clean('12345'))
391391
self.assertEqual(u'1234567890', f.clean('1234567890'))
392392
self.assertRaisesErrorWithMessage(ValidationError, "[u'Ensure this value has at most 10 characters (it has 11).']", f.clean, '12345678901')

tests/regressiontests/forms/localflavor/ar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
>>> f.clean('C1064AABB')
2929
Traceback (most recent call last):
3030
...
31-
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
31+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
3232
>>> f.clean('C1064AA')
3333
Traceback (most recent call last):
3434
...
@@ -44,7 +44,7 @@
4444
>>> f.clean('500')
4545
Traceback (most recent call last):
4646
...
47-
ValidationError: [u'Ensure this value has at least 4 characters (it has 3).']
47+
ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
4848
>>> f.clean('5PPP')
4949
Traceback (most recent call last):
5050
...
@@ -78,7 +78,7 @@
7878
>>> f.clean('C1064AABB')
7979
Traceback (most recent call last):
8080
...
81-
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
81+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
8282
>>> f.clean('C1064AA')
8383
Traceback (most recent call last):
8484
...
@@ -94,7 +94,7 @@
9494
>>> f.clean('500')
9595
Traceback (most recent call last):
9696
...
97-
ValidationError: [u'Ensure this value has at least 4 characters (it has 3).']
97+
ValidationError: [u'Ensure this value has at least 4 characters (it has 3).', u'Enter a postal code in the format NNNN or ANNNNAAA.']
9898
>>> f.clean('5PPP')
9999
Traceback (most recent call last):
100100
...

tests/regressiontests/forms/localflavor/is_.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
>>> f.clean('230880343')
1616
Traceback (most recent call last):
1717
...
18-
ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
18+
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.']
1919
>>> f.clean('230880343234')
2020
Traceback (most recent call last):
2121
...
22-
ValidationError: [u'Ensure this value has at most 11 characters (it has 12).']
22+
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.']
2323
>>> f.clean('abcdefghijk')
2424
Traceback (most recent call last):
2525
...
@@ -61,18 +61,18 @@
6161
>>> f.clean('123456')
6262
Traceback (most recent call last):
6363
...
64-
ValidationError: [u'Ensure this value has at least 7 characters (it has 6).']
64+
ValidationError: [u'Ensure this value has at least 7 characters (it has 6).', u'Enter a valid value.']
6565
>>> f.clean('123456555')
6666
Traceback (most recent call last):
6767
...
68-
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
68+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.']
6969
>>> f.clean('abcdefg')
7070
Traceback (most recent call last):
7171
ValidationError: [u'Enter a valid value.']
7272
>>> f.clean(' 1234567 ')
7373
Traceback (most recent call last):
7474
...
75-
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
75+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).', u'Enter a valid value.']
7676
>>> f.clean(' 12367 ')
7777
Traceback (most recent call last):
7878
...

0 commit comments

Comments
 (0)