Skip to content

Commit 40bb32b

Browse files
committed
Fixed #4469 -- Added slightly more informative error messages to max- and
min-length newform validation. Based on a patch from A. Murat Eren. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5686 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 3eb1071 commit 40bb32b

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

django/newforms/fields.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ def clean(self, value):
111111
if value in EMPTY_VALUES:
112112
return u''
113113
value = smart_unicode(value)
114-
if self.max_length is not None and len(value) > self.max_length:
115-
raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
116-
if self.min_length is not None and len(value) < self.min_length:
117-
raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
114+
value_length = len(value)
115+
if self.max_length is not None and value_length > self.max_length:
116+
raise ValidationError(ugettext(u'Ensure this value has at most %d characters (it has %d).') % (self.max_length, value_length))
117+
if self.min_length is not None and value_length < self.min_length:
118+
raise ValidationError(ugettext(u'Ensure this value has at least %d characters (it has %d).') % (self.min_length, value_length))
118119
return value
119120

120121
def widget_attrs(self, widget):

tests/regressiontests/forms/localflavor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -913,11 +913,11 @@
913913
>>> f.clean('375.788.573-000')
914914
Traceback (most recent call last):
915915
...
916-
ValidationError: [u'Ensure this value has at most 14 characters.']
916+
ValidationError: [u'Ensure this value has at most 14 characters (it has 15).']
917917
>>> f.clean('123.456.78')
918918
Traceback (most recent call last):
919919
...
920-
ValidationError: [u'Ensure this value has at least 11 characters.']
920+
ValidationError: [u'Ensure this value has at least 11 characters (it has 10).']
921921
>>> f.clean('123456789555')
922922
Traceback (most recent call last):
923923
...
@@ -1208,11 +1208,11 @@
12081208
>>> f.clean('230880343')
12091209
Traceback (most recent call last):
12101210
...
1211-
ValidationError: [u'Ensure this value has at least 10 characters.']
1211+
ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
12121212
>>> f.clean('230880343234')
12131213
Traceback (most recent call last):
12141214
...
1215-
ValidationError: [u'Ensure this value has at most 11 characters.']
1215+
ValidationError: [u'Ensure this value has at most 11 characters (it has 12).']
12161216
>>> f.clean('abcdefghijk')
12171217
Traceback (most recent call last):
12181218
...
@@ -1254,18 +1254,18 @@
12541254
>>> f.clean('123456')
12551255
Traceback (most recent call last):
12561256
...
1257-
ValidationError: [u'Ensure this value has at least 7 characters.']
1257+
ValidationError: [u'Ensure this value has at least 7 characters (it has 6).']
12581258
>>> f.clean('123456555')
12591259
Traceback (most recent call last):
12601260
...
1261-
ValidationError: [u'Ensure this value has at most 8 characters.']
1261+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
12621262
>>> f.clean('abcdefg')
12631263
Traceback (most recent call last):
12641264
ValidationError: [u'Enter a valid value.']
12651265
>>> f.clean(' 1234567 ')
12661266
Traceback (most recent call last):
12671267
...
1268-
ValidationError: [u'Ensure this value has at most 8 characters.']
1268+
ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
12691269
>>> f.clean(' 12367 ')
12701270
Traceback (most recent call last):
12711271
...

tests/regressiontests/forms/tests.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@
896896
>>> f.clean('1234567890a')
897897
Traceback (most recent call last):
898898
...
899-
ValidationError: [u'Ensure this value has at most 10 characters.']
899+
ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
900900
901901
CharField accepts an optional min_length parameter:
902902
>>> f = CharField(min_length=10, required=False)
@@ -905,7 +905,7 @@
905905
>>> f.clean('12345')
906906
Traceback (most recent call last):
907907
...
908-
ValidationError: [u'Ensure this value has at least 10 characters.']
908+
ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
909909
>>> f.clean('1234567890')
910910
u'1234567890'
911911
>>> f.clean('1234567890a')
@@ -919,7 +919,7 @@
919919
>>> f.clean('12345')
920920
Traceback (most recent call last):
921921
...
922-
ValidationError: [u'Ensure this value has at least 10 characters.']
922+
ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
923923
>>> f.clean('1234567890')
924924
u'1234567890'
925925
>>> f.clean('1234567890a')
@@ -1443,19 +1443,19 @@
14431443
>>> f.clean('123')
14441444
Traceback (most recent call last):
14451445
...
1446-
ValidationError: [u'Ensure this value has at least 5 characters.']
1446+
ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
14471447
>>> f.clean('abc')
14481448
Traceback (most recent call last):
14491449
...
1450-
ValidationError: [u'Ensure this value has at least 5 characters.']
1450+
ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
14511451
>>> f.clean('12345')
14521452
u'12345'
14531453
>>> f.clean('1234567890')
14541454
u'1234567890'
14551455
>>> f.clean('12345678901')
14561456
Traceback (most recent call last):
14571457
...
1458-
ValidationError: [u'Ensure this value has at most 10 characters.']
1458+
ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
14591459
>>> f.clean('12345a')
14601460
Traceback (most recent call last):
14611461
...
@@ -1512,13 +1512,13 @@
15121512
>>> f.clean('a@foo.com')
15131513
Traceback (most recent call last):
15141514
...
1515-
ValidationError: [u'Ensure this value has at least 10 characters.']
1515+
ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
15161516
>>> f.clean('alf@foo.com')
15171517
u'alf@foo.com'
15181518
>>> f.clean('alf123456788@foo.com')
15191519
Traceback (most recent call last):
15201520
...
1521-
ValidationError: [u'Ensure this value has at most 15 characters.']
1521+
ValidationError: [u'Ensure this value has at most 15 characters (it has 20).']
15221522
15231523
# URLField ##################################################################
15241524
@@ -1622,13 +1622,13 @@
16221622
>>> f.clean('http://f.com')
16231623
Traceback (most recent call last):
16241624
...
1625-
ValidationError: [u'Ensure this value has at least 15 characters.']
1625+
ValidationError: [u'Ensure this value has at least 15 characters (it has 12).']
16261626
>>> f.clean('http://example.com')
16271627
u'http://example.com'
16281628
>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
16291629
Traceback (most recent call last):
16301630
...
1631-
ValidationError: [u'Ensure this value has at most 20 characters.']
1631+
ValidationError: [u'Ensure this value has at most 20 characters (it has 37).']
16321632
16331633
# BooleanField ################################################################
16341634
@@ -1800,7 +1800,7 @@
18001800
>>> f.clean('longemailaddress@example.com')
18011801
Traceback (most recent call last):
18021802
...
1803-
ValidationError: [u'Ensure this value has at most 20 characters.']
1803+
ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
18041804
>>> f.clean('not an e-mail')
18051805
Traceback (most recent call last):
18061806
...
@@ -1820,7 +1820,7 @@
18201820
>>> f.clean('longemailaddress@example.com')
18211821
Traceback (most recent call last):
18221822
...
1823-
ValidationError: [u'Ensure this value has at most 20 characters.']
1823+
ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
18241824
>>> f.clean('not an e-mail')
18251825
Traceback (most recent call last):
18261826
...
@@ -3271,7 +3271,7 @@
32713271
<form action="" method="post">
32723272
<table>
32733273
<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
3274-
<tr><th>Username:</th><td><ul class="errorlist"><li>Ensure this value has at most 10 characters.</li></ul><input type="text" name="username" value="this-is-a-long-username" maxlength="10" /></td></tr>
3274+
<tr><th>Username:</th><td><ul class="errorlist"><li>Ensure this value has at most 10 characters (it has 23).</li></ul><input type="text" name="username" value="this-is-a-long-username" maxlength="10" /></td></tr>
32753275
<tr><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>
32763276
<tr><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>
32773277
</table>

0 commit comments

Comments
 (0)