Fixed #18724 -- Fixed IntegerField validation with value 0

This commit is contained in:
Brendan MacDonell 2012-08-06 10:42:21 +02:00 committed by Claude Paroz
parent 9a3026a920
commit ad237fb72f
2 changed files with 6 additions and 1 deletions

View File

@ -179,7 +179,8 @@ class Field(object):
if not self.editable:
# Skip validation for non-editable fields.
return
if self._choices and value:
if self._choices and value not in validators.EMPTY_VALUES:
for option_key, option_value in self.choices:
if isinstance(option_value, (list, tuple)):
# This is an optgroup, so look inside the group for

View File

@ -274,6 +274,10 @@ class ValidationTest(test.TestCase):
self.assertRaises(ValidationError, f.clean, None, None)
self.assertRaises(ValidationError, f.clean, '', None)
def test_integerfield_validates_zero_against_choices(self):
f = models.IntegerField(choices=((1, 1),))
self.assertRaises(ValidationError, f.clean, '0', None)
def test_charfield_raises_error_on_empty_input(self):
f = models.CharField(null=False)
self.assertRaises(ValidationError, f.clean, None, None)