Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2364: added NumberIsInRange validator. Thanks, Matt McClanahan.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4039 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3a0733e12c2b0a03f80ff828f49f2cf6a258c138 1 parent 4cb2247
Jacob Kaplan-Moss authored November 07, 2006
32  django/core/validators.py
@@ -353,6 +353,38 @@ def __call__(self, field_data, all_data):
353 353
             if field_name != self.field_name and value == field_data:
354 354
                 raise ValidationError, self.error_message
355 355
 
  356
+class NumberIsInRange(object):
  357
+    """
  358
+    Validator that tests if a value is in a range (inclusive).
  359
+    """
  360
+    def __init__(self, lower=None, upper=None, error_message=''):
  361
+        self.lower, self.upper = lower, upper
  362
+        if not error_message:
  363
+            if lower and upper:
  364
+                self.error_message = gettext("This value must be between %s and %s.") % (lower, upper)
  365
+            elif lower:
  366
+                self.error_message = gettext("This value must be at least %s.") % lower
  367
+            elif upper:
  368
+                self.error_message = gettext("This value must be no more than %s.") % upper
  369
+        else:
  370
+            self.error_message = error_message
  371
+
  372
+    def __call__(self, field_data, all_data):
  373
+        # Try to make the value numeric. If this fails, we assume another 
  374
+        # validator will catch the problem.
  375
+        try:
  376
+            val = float(field_data)
  377
+        except ValueError:
  378
+            return
  379
+            
  380
+        # Now validate
  381
+        if self.lower and self.upper and (val < self.lower or val > self.upper):
  382
+            raise ValidationError(self.error_message)
  383
+        elif self.lower and val < self.lower:
  384
+            raise ValidationError(self.error_message)
  385
+        elif self.upper and val > self.upper:
  386
+            raise ValidationError(self.error_message)
  387
+
356 388
 class IsAPowerOf(object):
357 389
     """
358 390
     >>> v = IsAPowerOf(2)
9  docs/forms.txt
@@ -610,6 +610,15 @@ fails. If no message is passed in, a default message is used.
610 610
     string "123" is less than the string "2", for example. If you don't want
611 611
     string comparison here, you will need to write your own validator.
612 612
 
  613
+``NumberIsInRange``
  614
+    Takes two boundary number, ``lower`` and ``upper`` and checks that the
  615
+    field is greater than ``lower`` (if given) and less than ``upper`` (if
  616
+    given).  
  617
+    
  618
+    Both checks are inclusive; that is, ``NumberIsInRange(10, 20)`` will allow
  619
+    values of both 10 and 20.  This validator only checks numeric fields
  620
+    (i.e. floats and integer fields).
  621
+
613 622
 ``IsAPowerOf``
614 623
     Takes an integer argument and when called as a validator, checks that the
615 624
     field being validated is a power of the integer.

0 notes on commit 3a0733e

Please sign in to comment.
Something went wrong with that request. Please try again.