Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Implemented min_value and max_value options for IntegerField

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 546297be27839f4ed3d627ff8fde04ee41d1667d 1 parent 26489d4
Adrian Holovaty authored
11  django/newforms/fields.py
@@ -93,6 +93,10 @@ def widget_attrs(self, widget):
93 93
             return {'maxlength': str(self.max_length)}
94 94
 
95 95
 class IntegerField(Field):
  96
+    def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None):
  97
+        self.max_value, self.min_value = max_value, min_value
  98
+        Field.__init__(self, required, widget, label)
  99
+
96 100
     def clean(self, value):
97 101
         """
98 102
         Validates that int() can be called on the input. Returns the result
@@ -102,9 +106,14 @@ def clean(self, value):
102 106
         if not self.required and value in EMPTY_VALUES:
103 107
             return u''
104 108
         try:
105  
-            return int(value)
  109
+            value = int(value)
106 110
         except (ValueError, TypeError):
107 111
             raise ValidationError(gettext(u'Enter a whole number.'))
  112
+        if self.max_value is not None and value > self.max_value:
  113
+            raise ValidationError(gettext(u'Ensure this value is less than or equal to %s.') % self.max_value)
  114
+        if self.min_value is not None and value < self.min_value:
  115
+            raise ValidationError(gettext(u'Ensure this value is greater than or equal to %s.') % self.min_value)
  116
+        return value
108 117
 
109 118
 DEFAULT_DATE_INPUT_FORMATS = (
110 119
     '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
65  tests/regressiontests/forms/tests.py
@@ -769,6 +769,71 @@
769 769
 ...
770 770
 ValidationError: [u'Enter a whole number.']
771 771
 
  772
+IntegerField accepts an optional max_value parameter:
  773
+>>> f = IntegerField(max_value=10)
  774
+>>> f.clean(None)
  775
+Traceback (most recent call last):
  776
+...
  777
+ValidationError: [u'This field is required.']
  778
+>>> f.clean(1)
  779
+1
  780
+>>> f.clean(10)
  781
+10
  782
+>>> f.clean(11)
  783
+Traceback (most recent call last):
  784
+...
  785
+ValidationError: [u'Ensure this value is less than or equal to 10.']
  786
+>>> f.clean('10')
  787
+10
  788
+>>> f.clean('11')
  789
+Traceback (most recent call last):
  790
+...
  791
+ValidationError: [u'Ensure this value is less than or equal to 10.']
  792
+
  793
+IntegerField accepts an optional min_value parameter:
  794
+>>> f = IntegerField(min_value=10)
  795
+>>> f.clean(None)
  796
+Traceback (most recent call last):
  797
+...
  798
+ValidationError: [u'This field is required.']
  799
+>>> f.clean(1)
  800
+Traceback (most recent call last):
  801
+...
  802
+ValidationError: [u'Ensure this value is greater than or equal to 10.']
  803
+>>> f.clean(10)
  804
+10
  805
+>>> f.clean(11)
  806
+11
  807
+>>> f.clean('10')
  808
+10
  809
+>>> f.clean('11')
  810
+11
  811
+
  812
+min_value and max_value can be used together:
  813
+>>> f = IntegerField(min_value=10, max_value=20)
  814
+>>> f.clean(None)
  815
+Traceback (most recent call last):
  816
+...
  817
+ValidationError: [u'This field is required.']
  818
+>>> f.clean(1)
  819
+Traceback (most recent call last):
  820
+...
  821
+ValidationError: [u'Ensure this value is greater than or equal to 10.']
  822
+>>> f.clean(10)
  823
+10
  824
+>>> f.clean(11)
  825
+11
  826
+>>> f.clean('10')
  827
+10
  828
+>>> f.clean('11')
  829
+11
  830
+>>> f.clean(20)
  831
+20
  832
+>>> f.clean(21)
  833
+Traceback (most recent call last):
  834
+...
  835
+ValidationError: [u'Ensure this value is less than or equal to 20.']
  836
+
772 837
 # DateField ###################################################################
773 838
 
774 839
 >>> import datetime

0 notes on commit 546297b

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