diff --git a/django/newforms/fields.py b/django/newforms/fields.py index dfe46a2e3e..6734c5450e 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -535,13 +535,17 @@ class BooleanField(Field): def clean(self, value): """Returns a Python boolean object.""" - super(BooleanField, self).clean(value) # Explicitly check for the string 'False', which is what a hidden field # will submit for False. Because bool("True") == True, we don't need to # handle that explicitly. if value == 'False': - return False - return bool(value) + value = False + else: + value = bool(value) + super(BooleanField, self).clean(value) + if not value and self.required: + raise ValidationError(self.error_messages['required']) + return value class NullBooleanField(BooleanField): """ diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 950b1761a5..c9f3efdbda 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -937,18 +937,24 @@ ValidationError: [u'This field is required.'] >>> f.clean(True) True >>> f.clean(False) -False +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] >>> f.clean(1) True >>> f.clean(0) -False +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] >>> f.clean('Django rocks') True >>> f.clean('True') True >>> f.clean('False') -False +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] >>> f = BooleanField(required=False) >>> f.clean('')