Ticket #4469: newforms-field_length-validation.diff

File newforms-field_length-validation.diff, 1.2 KB (added by A. Murat Eren <meren@…>, 17 years ago)
  • newforms/fields.py

     
    106106        if value in EMPTY_VALUES:
    107107            return u''
    108108        value = smart_unicode(value)
    109         if self.max_length is not None and len(value) > self.max_length:
    110             raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
    111         if self.min_length is not None and len(value) < self.min_length:
    112             raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length)
     109        value_length = len(value)
     110        if self.max_length is not None and value_length > self.max_length:
     111            raise ValidationError(gettext(u'Ensure this value has at most %d characters (it has %d).') % (self.max_length, value_length))
     112        if self.min_length is not None and value_length < self.min_length:
     113            raise ValidationError(gettext(u'Ensure this value has at least %d characters (it has %d).') % (self.min_length, value_length))
    113114        return value
    114115
    115116    def widget_attrs(self, widget):
Back to Top