From 546297be27839f4ed3d627ff8fde04ee41d1667d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 15 Dec 2006 23:18:47 +0000 Subject: [PATCH] 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 --- django/newforms/fields.py | 11 ++++- tests/regressiontests/forms/tests.py | 65 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/django/newforms/fields.py b/django/newforms/fields.py index af2533cec5..a32018e108 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -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' diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index e450561bd0..058a3d8958 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -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