Ticket #2364: numericrangevalidator.diff

File numericrangevalidator.diff, 1.4 KB (added by Matt McClanahan, 18 years ago)
  • django/core/validators.py

     
    324324            if field_name != self.field_name and value == field_data:
    325325                raise ValidationError, self.error_message
    326326
     327class NumericRangeValidator(object):
     328    def __init__(self, lower_bound=None, upper_bound=None, error_message=None):
     329        self.lower_bound, self.upper_bound = lower_bound, upper_bound
     330        gt_msg = ''
     331        if not error_message:
     332            if lower_bound and upper_bound:
     333                gt_msg = "This value must be between %s and %s." % (lower_bound, upper_bound)
     334            elif lower_bound:
     335                gt_msg = "This value must be at least %s." % lower_bound
     336            elif upper_bound:
     337                gt_msg = "This value must be no more than %s." % upper_bound
     338        self.error_message = error_message or gettext_lazy(gt_msg)
     339
     340    def __call__(self, field_data, all_data):
     341        val = float(field_data)
     342        crit1 = crit2 = True
     343        if self.lower_bound:
     344            crit1 = val >= self.lower_bound
     345        if self.upper_bound:
     346            crit2 = val <= self.upper_bound
     347        if not (crit1 and crit2):
     348            raise ValidationError, self.error_message
     349
    327350class IsAPowerOf(object):
    328351    """
    329352    >>> v = IsAPowerOf(2)
Back to Top