Back to index

plone3  3.1.7
RangeValidator.py
Go to the documentation of this file.
00001 try:
00002     from Products.validation.interfaces.IValidator import IValidator
00003 except ImportError:
00004     import sys, os
00005     sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
00006     from interfaces.IValidator import IValidator
00007     del sys, os
00008 
00009 class RangeValidator:
00010     __implements__ = IValidator
00011 
00012     def __init__(self, name, minval=0.0, maxval=0.0, title='', description=''):
00013         self.name = name
00014         self.minval = minval
00015         self.maxval = maxval
00016         self.title = title or name
00017         self.description = description
00018 
00019     def __call__(self, value, *args, **kwargs):
00020         if len(args)>=1:
00021             minval=args[0]
00022         else:
00023             minval=self.minval
00024             
00025         if len(args)>=2:
00026             maxval=args[1]
00027         else:
00028             maxval=self.maxval
00029 
00030         assert(minval <= maxval)
00031         try:
00032             nval = float(value)
00033         except ValueError:
00034             return ("Validation failed(%(name)s): could not convert '%(value)r' to number" %
00035                     { 'name' : self.name, 'value': value})
00036         if minval <= nval < maxval:
00037             return 1
00038 
00039         return ("Validation failed(%(name)s): '%(value)s' out of range(%(min)s, %(max)s)" %
00040                 { 'name' : self.name, 'value': value, 'min' : minval, 'max' : maxval,})