Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 40bb32b5a47c4aa94434d884ff0fca54337657f1 1 parent 3eb1071
Malcolm Tredinnick authored July 13, 2007
9  django/newforms/fields.py
@@ -111,10 +111,11 @@ def clean(self, value):
111 111
         if value in EMPTY_VALUES:
112 112
             return u''
113 113
         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))
118 119
         return value
119 120
 
120 121
     def widget_attrs(self, widget):
14  tests/regressiontests/forms/localflavor.py
@@ -913,11 +913,11 @@
913 913
 >>> f.clean('375.788.573-000')
914 914
 Traceback (most recent call last):
915 915
 ...
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).']
917 917
 >>> f.clean('123.456.78')
918 918
 Traceback (most recent call last):
919 919
 ...
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).']
921 921
 >>> f.clean('123456789555')
922 922
 Traceback (most recent call last):
923 923
 ...
@@ -1208,11 +1208,11 @@
1208 1208
 >>> f.clean('230880343')
1209 1209
 Traceback (most recent call last):
1210 1210
 ...
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).']
1212 1212
 >>> f.clean('230880343234')
1213 1213
 Traceback (most recent call last):
1214 1214
 ...
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).']
1216 1216
 >>> f.clean('abcdefghijk')
1217 1217
 Traceback (most recent call last):
1218 1218
 ...
@@ -1254,18 +1254,18 @@
1254 1254
 >>> f.clean('123456')
1255 1255
 Traceback (most recent call last):
1256 1256
 ...
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).']
1258 1258
 >>> f.clean('123456555')
1259 1259
 Traceback (most recent call last):
1260 1260
 ...
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).']
1262 1262
 >>> f.clean('abcdefg')
1263 1263
 Traceback (most recent call last):
1264 1264
 ValidationError: [u'Enter a valid value.']
1265 1265
 >>> f.clean(' 1234567 ')
1266 1266
 Traceback (most recent call last):
1267 1267
 ...
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).']
1269 1269
 >>> f.clean(' 12367  ')
1270 1270
 Traceback (most recent call last):
1271 1271
 ...
26  tests/regressiontests/forms/tests.py
@@ -896,7 +896,7 @@
896 896
 >>> f.clean('1234567890a')
897 897
 Traceback (most recent call last):
898 898
 ...
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).']
900 900
 
901 901
 CharField accepts an optional min_length parameter:
902 902
 >>> f = CharField(min_length=10, required=False)
@@ -905,7 +905,7 @@
905 905
 >>> f.clean('12345')
906 906
 Traceback (most recent call last):
907 907
 ...
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).']
909 909
 >>> f.clean('1234567890')
910 910
 u'1234567890'
911 911
 >>> f.clean('1234567890a')
@@ -919,7 +919,7 @@
919 919
 >>> f.clean('12345')
920 920
 Traceback (most recent call last):
921 921
 ...
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).']
923 923
 >>> f.clean('1234567890')
924 924
 u'1234567890'
925 925
 >>> f.clean('1234567890a')
@@ -1443,11 +1443,11 @@
1443 1443
 >>> f.clean('123')
1444 1444
 Traceback (most recent call last):
1445 1445
 ...
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).']
1447 1447
 >>> f.clean('abc')
1448 1448
 Traceback (most recent call last):
1449 1449
 ...
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).']
1451 1451
 >>> f.clean('12345')
1452 1452
 u'12345'
1453 1453
 >>> f.clean('1234567890')
@@ -1455,7 +1455,7 @@
1455 1455
 >>> f.clean('12345678901')
1456 1456
 Traceback (most recent call last):
1457 1457
 ...
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).']
1459 1459
 >>> f.clean('12345a')
1460 1460
 Traceback (most recent call last):
1461 1461
 ...
@@ -1512,13 +1512,13 @@
1512 1512
 >>> f.clean('a@foo.com')
1513 1513
 Traceback (most recent call last):
1514 1514
 ...
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).']
1516 1516
 >>> f.clean('alf@foo.com')
1517 1517
 u'alf@foo.com'
1518 1518
 >>> f.clean('alf123456788@foo.com')
1519 1519
 Traceback (most recent call last):
1520 1520
 ...
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).']
1522 1522
 
1523 1523
 # URLField ##################################################################
1524 1524
 
@@ -1622,13 +1622,13 @@
1622 1622
 >>> f.clean('http://f.com')
1623 1623
 Traceback (most recent call last):
1624 1624
 ...
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).']
1626 1626
 >>> f.clean('http://example.com')
1627 1627
 u'http://example.com'
1628 1628
 >>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
1629 1629
 Traceback (most recent call last):
1630 1630
 ...
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).']
1632 1632
 
1633 1633
 # BooleanField ################################################################
1634 1634
 
@@ -1800,7 +1800,7 @@
1800 1800
 >>> f.clean('longemailaddress@example.com')
1801 1801
 Traceback (most recent call last):
1802 1802
 ...
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).']
1804 1804
 >>> f.clean('not an e-mail')
1805 1805
 Traceback (most recent call last):
1806 1806
 ...
@@ -1820,7 +1820,7 @@
1820 1820
 >>> f.clean('longemailaddress@example.com')
1821 1821
 Traceback (most recent call last):
1822 1822
 ...
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).']
1824 1824
 >>> f.clean('not an e-mail')
1825 1825
 Traceback (most recent call last):
1826 1826
 ...
@@ -3271,7 +3271,7 @@
3271 3271
 <form action="" method="post">
3272 3272
 <table>
3273 3273
 <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>
3275 3275
 <tr><th>Password1:</th><td><input type="password" name="password1" value="foo" /></td></tr>
3276 3276
 <tr><th>Password2:</th><td><input type="password" name="password2" value="bar" /></td></tr>
3277 3277
 </table>

0 notes on commit 40bb32b

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