Author: ianb
Date: 2008-05-13 15:37:19 -0600 (Tue, 13 May 2008)
New Revision: 3416

Modified:
   FormEncode/trunk/docs/news.txt
   FormEncode/trunk/formencode/validators.py
Log:
patch from Shannon JJ Behrens, adding min/max support to Int and Number

Modified: FormEncode/trunk/docs/news.txt
===================================================================
--- FormEncode/trunk/docs/news.txt      2008-05-13 21:35:50 UTC (rev 3415)
+++ FormEncode/trunk/docs/news.txt      2008-05-13 21:37:19 UTC (rev 3416)
@@ -18,6 +18,9 @@
 * In ``formencode.htmlfill``, non-string values are compared usefully
   (e.g., a select box with integer values).
 
+* The validators ``Int`` and ``Number`` both take min/max arguments
+  (from Shannon Behrens).
+
 1.0.1
 -----
 

Modified: FormEncode/trunk/formencode/validators.py
===================================================================
--- FormEncode/trunk/formencode/validators.py   2008-05-13 21:35:50 UTC (rev 
3415)
+++ FormEncode/trunk/formencode/validators.py   2008-05-13 21:37:19 UTC (rev 
3416)
@@ -909,11 +909,41 @@
     def empty_value(self, value):
         return False
 
-class Int(FancyValidator):
+class RangeValidator(FancyValidator):
 
+    """This is an abstract base class for Int and Number.
+
+    It verifies that a value is within range.  It accepts min and max
+    values in the constructor.
+
+    (Since this is an abstract base class, the tests are in Int and
+    Number.)
+
     """
-    Convert a value to an integer.
 
+    messages = {
+        'tooLow': _("Please enter a number that is %(min)s or greater"),
+        'tooHigh': _("Please enter a number that is %(max)s or smaller"),
+    }
+
+    min = None
+    max = None
+
+    def validate_python(self, value, state):
+        if self.min is not None:
+            if value < self.min:
+                msg = self.message("tooLow", state, min=self.min)
+                raise Invalid(msg, value, state)
+        if self.max is not None:
+            if value > self.max:
+                msg = self.message("tooHigh", state, max=self.max)
+                raise Invalid(msg, value, state)
+
+
+class Int(RangeValidator):
+
+    """Convert a value to an integer.
+
     Example::
 
         >>> Int.to_python('10')
@@ -922,24 +952,19 @@
         Traceback (most recent call last):
             ...
         Invalid: Please enter an integer value
+        >>> Int(min=5).to_python('6')
+        6
+        >>> Int(max=10).to_python('11')
+        Traceback (most recent call last):
+            ...
+        Invalid: Please enter a number that is 10 or smaller
+
     """
 
     messages = {
-        'integer': _("Please enter an integer value"),
-        'tooLow': _("Please enter an integer above %(min)i"),
-        'tooHigh': _("Please enter an integer below %(max)i"),
-        }
+        'integer': _("Please enter an integer value")
+    }
 
-    min = None
-    max = None
-    
-    def __initargs__(self, args):
-        if self.min != None:
-            self.min = int(self.min)
-        if self.max != None:
-            self.max = int(self.max)
-
-
     def _to_python(self, value, state):
         try:
             return int(value)
@@ -947,24 +972,17 @@
             raise Invalid(self.message('integer', state),
                           value, state)
 
-    def validate_python(self, value, state):
-        if self.min != None and value < self.min:
-            msg = self.message("tooLow", state, min=self.min)
-            raise Invalid(msg, value, state)
-        if self.max != None and value > self.max:
-            msg = self.message("tooHigh", state, max=self.max)
-            raise Invalid(msg, value, state)
-
     _from_python = _to_python
 
-class Number(FancyValidator):
 
-    """
-    Convert a value to a float or integer.  Tries to convert it to
-    an integer if no information is lost.
+class Number(RangeValidator):
 
-    ::
+    """Convert a value to a float or integer.
 
+    Tries to convert it to an integer if no information is lost.
+
+    Example::
+
         >>> Number.to_python('10')
         10
         >>> Number.to_python('10.5')
@@ -973,13 +991,19 @@
         Traceback (most recent call last):
             ...
         Invalid: Please enter a number
+        >>> Number(min=5).to_python('6.5')
+        6.5
+        >>> Number(max=10.5).to_python('11.5')
+        Traceback (most recent call last):
+            ...
+        Invalid: Please enter a number that is 10.5 or smaller
 
     """
-    
+
     messages = {
-        'number': _("Please enter a number"),
-        }
-    
+        'number': _("Please enter a number")
+    }
+
     def _to_python(self, value, state):
         try:
             value = float(value)
@@ -990,6 +1014,7 @@
             raise Invalid(self.message('number', state),
                           value, state)
 
+
 class String(FancyValidator):
     """
     Converts things to string, but treats empty things as the empty


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
FormEncode-CVS mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-cvs

Reply via email to