Ticket #16617: length-validators.diff

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

Patch shows string rather than length for min|max length

  • django/core/validators.py

    diff --git a/django/core/validators.py b/django/core/validators.py
    index 458f419..efb0a41 100644
    a b class BaseValidator(object):  
    196196
    197197    def __call__(self, value):
    198198        cleaned = self.clean(value)
    199         params = {'limit_value': self.limit_value, 'show_value': cleaned}
     199        params = {'limit_value': self.limit_value, 'show_value': cleaned[:self.limit_value] if isinstance(cleaned, basestring) else cleaned}
    200200        if self.compare(cleaned, self.limit_value):
    201201            raise ValidationError(
    202202                self.message % params,
    class MinValueValidator(BaseValidator):  
    215215    code = 'min_value'
    216216
    217217class MinLengthValidator(BaseValidator):
    218     compare = lambda self, a, b: a < b
    219     clean   = lambda self, x: len(x)
    220     message = _(u'Ensure this value has at least %(limit_value)d characters (it has %(show_value)d).')
     218    compare = lambda self, a, b: len(a) < b
     219    message = _(u'Ensure this value has at least %(limit_value)d characters (%(show_value)s).')
    221220    code = 'min_length'
    222221
    223222class MaxLengthValidator(BaseValidator):
    224     compare = lambda self, a, b: a > b
    225     clean   = lambda self, x: len(x)
    226     message = _(u'Ensure this value has at most %(limit_value)d characters (it has %(show_value)d).')
     223    compare = lambda self, a, b: len(a) > b
     224    message = _(u'Ensure this value has at most %(limit_value)d characters (%(show_value)s...).')
    227225    code = 'max_length'
    228226
Back to Top