Author: mtredinnick
Date: 2007-07-13 09:13:35 -0500 (Fri, 13 Jul 2007)
New Revision: 5686
Modified:
django/trunk/django/newforms/fields.py
django/trunk/tests/regressiontests/forms/localflavor.py
django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #4469 -- Added slightly more informative error messages to max- and
min-length newform validation. Based on a patch from A. Murat Eren.
Modified: django/trunk/django/newforms/fields.py
===================================================================
--- django/trunk/django/newforms/fields.py 2007-07-13 13:15:35 UTC (rev
5685)
+++ django/trunk/django/newforms/fields.py 2007-07-13 14:13:35 UTC (rev
5686)
@@ -111,10 +111,11 @@
if value in EMPTY_VALUES:
return u''
value = smart_unicode(value)
- if self.max_length is not None and len(value) > self.max_length:
- raise ValidationError(ugettext(u'Ensure this value has at most %d
characters.') % self.max_length)
- if self.min_length is not None and len(value) < self.min_length:
- raise ValidationError(ugettext(u'Ensure this value has at least %d
characters.') % self.min_length)
+ value_length = len(value)
+ if self.max_length is not None and value_length > self.max_length:
+ raise ValidationError(ugettext(u'Ensure this value has at most %d
characters (it has %d).') % (self.max_length, value_length))
+ if self.min_length is not None and value_length < self.min_length:
+ raise ValidationError(ugettext(u'Ensure this value has at least %d
characters (it has %d).') % (self.min_length, value_length))
return value
def widget_attrs(self, widget):
Modified: django/trunk/tests/regressiontests/forms/localflavor.py
===================================================================
--- django/trunk/tests/regressiontests/forms/localflavor.py 2007-07-13
13:15:35 UTC (rev 5685)
+++ django/trunk/tests/regressiontests/forms/localflavor.py 2007-07-13
14:13:35 UTC (rev 5686)
@@ -913,11 +913,11 @@
>>> f.clean('375.788.573-000')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 14 characters.']
+ValidationError: [u'Ensure this value has at most 14 characters (it has 15).']
>>> f.clean('123.456.78')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 11 characters.']
+ValidationError: [u'Ensure this value has at least 11 characters (it has 10).']
>>> f.clean('123456789555')
Traceback (most recent call last):
...
@@ -1208,11 +1208,11 @@
>>> f.clean('230880343')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 10 characters.']
+ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
>>> f.clean('230880343234')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 11 characters.']
+ValidationError: [u'Ensure this value has at most 11 characters (it has 12).']
>>> f.clean('abcdefghijk')
Traceback (most recent call last):
...
@@ -1254,18 +1254,18 @@
>>> f.clean('123456')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 7 characters.']
+ValidationError: [u'Ensure this value has at least 7 characters (it has 6).']
>>> f.clean('123456555')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 8 characters.']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
>>> 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.']
+ValidationError: [u'Ensure this value has at most 8 characters (it has 9).']
>>> f.clean(' 12367 ')
Traceback (most recent call last):
...
Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py 2007-07-13 13:15:35 UTC
(rev 5685)
+++ django/trunk/tests/regressiontests/forms/tests.py 2007-07-13 14:13:35 UTC
(rev 5686)
@@ -896,7 +896,7 @@
>>> f.clean('1234567890a')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 10 characters.']
+ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
CharField accepts an optional min_length parameter:
>>> f = CharField(min_length=10, required=False)
@@ -905,7 +905,7 @@
>>> f.clean('12345')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 10 characters.']
+ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
>>> f.clean('1234567890')
u'1234567890'
>>> f.clean('1234567890a')
@@ -919,7 +919,7 @@
>>> f.clean('12345')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 10 characters.']
+ValidationError: [u'Ensure this value has at least 10 characters (it has 5).']
>>> f.clean('1234567890')
u'1234567890'
>>> f.clean('1234567890a')
@@ -1443,11 +1443,11 @@
>>> f.clean('123')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 5 characters.']
+ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
>>> f.clean('abc')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 5 characters.']
+ValidationError: [u'Ensure this value has at least 5 characters (it has 3).']
>>> f.clean('12345')
u'12345'
>>> f.clean('1234567890')
@@ -1455,7 +1455,7 @@
>>> f.clean('12345678901')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 10 characters.']
+ValidationError: [u'Ensure this value has at most 10 characters (it has 11).']
>>> f.clean('12345a')
Traceback (most recent call last):
...
@@ -1512,13 +1512,13 @@
>>> f.clean('[EMAIL PROTECTED]')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 10 characters.']
+ValidationError: [u'Ensure this value has at least 10 characters (it has 9).']
>>> f.clean('[EMAIL PROTECTED]')
u'[EMAIL PROTECTED]'
>>> f.clean('[EMAIL PROTECTED]')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 15 characters.']
+ValidationError: [u'Ensure this value has at most 15 characters (it has 20).']
# URLField ##################################################################
@@ -1622,13 +1622,13 @@
>>> f.clean('http://f.com')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at least 15 characters.']
+ValidationError: [u'Ensure this value has at least 15 characters (it has 12).']
>>> f.clean('http://example.com')
u'http://example.com'
>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 20 characters.']
+ValidationError: [u'Ensure this value has at most 20 characters (it has 37).']
# BooleanField ################################################################
@@ -1800,7 +1800,7 @@
>>> f.clean('[EMAIL PROTECTED]')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 20 characters.']
+ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
>>> f.clean('not an e-mail')
Traceback (most recent call last):
...
@@ -1820,7 +1820,7 @@
>>> f.clean('[EMAIL PROTECTED]')
Traceback (most recent call last):
...
-ValidationError: [u'Ensure this value has at most 20 characters.']
+ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
>>> f.clean('not an e-mail')
Traceback (most recent call last):
...
@@ -3271,7 +3271,7 @@
<form action="" method="post">
<table>
<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords
match.</li></ul></td></tr>
-<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>
+<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>
<tr><th>Password1:</th><td><input type="password" name="password1" value="foo"
/></td></tr>
<tr><th>Password2:</th><td><input type="password" name="password2" value="bar"
/></td></tr>
</table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---