Fixed #3293 -- newforms: Changed IntegerField.clean() to return None if field is not required and empty. Thanks, Honza Kral

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4312 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-01-13 05:08:07 +00:00
parent a0137c41f3
commit 6b31f95516
2 changed files with 6 additions and 4 deletions

View File

@ -114,11 +114,11 @@ class IntegerField(Field):
def clean(self, value): def clean(self, value):
""" """
Validates that int() can be called on the input. Returns the result Validates that int() can be called on the input. Returns the result
of int(). of int(). Returns None for empty values.
""" """
super(IntegerField, self).clean(value) super(IntegerField, self).clean(value)
if not self.required and value in EMPTY_VALUES: if not self.required and value in EMPTY_VALUES:
return u'' return None
try: try:
value = int(value) value = int(value)
except (ValueError, TypeError): except (ValueError, TypeError):

View File

@ -810,9 +810,11 @@ ValidationError: [u'Enter a whole number.']
>>> f = IntegerField(required=False) >>> f = IntegerField(required=False)
>>> f.clean('') >>> f.clean('')
u'' >>> repr(f.clean(''))
'None'
>>> f.clean(None) >>> f.clean(None)
u'' >>> repr(f.clean(None))
'None'
>>> f.clean('1') >>> f.clean('1')
1 1
>>> isinstance(f.clean('1'), int) >>> isinstance(f.clean('1'), int)