Code

Ticket #16617: length-validators.diff

File length-validators.diff, 1.5 KB (added by version2beta, 3 years ago)

Patch shows string rather than length for min|max length

Line 
1diff --git a/django/core/validators.py b/django/core/validators.py
2index 458f419..efb0a41 100644
3--- a/django/core/validators.py
4+++ b/django/core/validators.py
5@@ -196,7 +196,7 @@ class BaseValidator(object):
6 
7     def __call__(self, value):
8         cleaned = self.clean(value)
9-        params = {'limit_value': self.limit_value, 'show_value': cleaned}
10+        params = {'limit_value': self.limit_value, 'show_value': cleaned[:self.limit_value] if isinstance(cleaned, basestring) else cleaned}
11         if self.compare(cleaned, self.limit_value):
12             raise ValidationError(
13                 self.message % params,
14@@ -215,14 +215,12 @@ class MinValueValidator(BaseValidator):
15     code = 'min_value'
16 
17 class MinLengthValidator(BaseValidator):
18-    compare = lambda self, a, b: a < b
19-    clean   = lambda self, x: len(x)
20-    message = _(u'Ensure this value has at least %(limit_value)d characters (it has %(show_value)d).')
21+    compare = lambda self, a, b: len(a) < b
22+    message = _(u'Ensure this value has at least %(limit_value)d characters (%(show_value)s).')
23     code = 'min_length'
24 
25 class MaxLengthValidator(BaseValidator):
26-    compare = lambda self, a, b: a > b
27-    clean   = lambda self, x: len(x)
28-    message = _(u'Ensure this value has at most %(limit_value)d characters (it has %(show_value)d).')
29+    compare = lambda self, a, b: len(a) > b
30+    message = _(u'Ensure this value has at most %(limit_value)d characters (%(show_value)s...).')
31     code = 'max_length'
32