0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

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
This commit is contained in:
Adrian Holovaty 2006-12-15 23:18:47 +00:00
parent 26489d4e2a
commit 546297be27
2 changed files with 75 additions and 1 deletions

View File

@ -93,6 +93,10 @@ class CharField(Field):
return {'maxlength': str(self.max_length)}
class IntegerField(Field):
def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None):
self.max_value, self.min_value = max_value, min_value
Field.__init__(self, required, widget, label)
def clean(self, value):
"""
Validates that int() can be called on the input. Returns the result
@ -102,9 +106,14 @@ class IntegerField(Field):
if not self.required and value in EMPTY_VALUES:
return u''
try:
return int(value)
value = int(value)
except (ValueError, TypeError):
raise ValidationError(gettext(u'Enter a whole number.'))
if self.max_value is not None and value > self.max_value:
raise ValidationError(gettext(u'Ensure this value is less than or equal to %s.') % self.max_value)
if self.min_value is not None and value < self.min_value:
raise ValidationError(gettext(u'Ensure this value is greater than or equal to %s.') % self.min_value)
return value
DEFAULT_DATE_INPUT_FORMATS = (
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'

View File

@ -769,6 +769,71 @@ Traceback (most recent call last):
...
ValidationError: [u'Enter a whole number.']
IntegerField accepts an optional max_value parameter:
>>> f = IntegerField(max_value=10)
>>> f.clean(None)
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f.clean(1)
1
>>> f.clean(10)
10
>>> f.clean(11)
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value is less than or equal to 10.']
>>> f.clean('10')
10
>>> f.clean('11')
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value is less than or equal to 10.']
IntegerField accepts an optional min_value parameter:
>>> f = IntegerField(min_value=10)
>>> f.clean(None)
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f.clean(1)
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value is greater than or equal to 10.']
>>> f.clean(10)
10
>>> f.clean(11)
11
>>> f.clean('10')
10
>>> f.clean('11')
11
min_value and max_value can be used together:
>>> f = IntegerField(min_value=10, max_value=20)
>>> f.clean(None)
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f.clean(1)
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value is greater than or equal to 10.']
>>> f.clean(10)
10
>>> f.clean(11)
11
>>> f.clean('10')
10
>>> f.clean('11')
11
>>> f.clean(20)
20
>>> f.clean(21)
Traceback (most recent call last):
...
ValidationError: [u'Ensure this value is less than or equal to 20.']
# DateField ###################################################################
>>> import datetime