Ticket #4415: positiveintegerfield2.2.diff

File positiveintegerfield2.2.diff, 945 bytes (added by Will Hardy <will@…>, 17 years ago)

new class: newforms.PositiveIntegerField

  • django/newforms/fields.py

     
    139139            raise ValidationError(ugettext(u'Ensure this value is greater than or equal to %s.') % self.min_value)
    140140        return value
    141141
     142class PositiveIntegerField(IntegerField):
     143    def clean(self, value):
     144        """
     145        Validates that the value is positive.
     146        Returns the result of int().  Returns None for empty values.
     147        """
     148        value = super(PositiveIntegerField, self).clean(value)
     149        if value is not None and value < 0:
     150            raise ValidationError(ugettext(u'Ensure this number is positive.'))
     151        return value
     152
    142153class FloatField(Field):
    143154    def __init__(self, max_value=None, min_value=None, *args, **kwargs):
    144155        self.max_value, self.min_value = max_value, min_value
Back to Top