diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 9bb2ced583..9b1253de1f 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -536,11 +536,12 @@ class BooleanField(Field): widget = CheckboxInput def clean(self, value): - "Returns a Python boolean object." + """Returns a Python boolean object.""" super(BooleanField, self).clean(value) - # Explicitly check for the string '0', which is what as hidden field - # will submit for False. - if value == '0': + # Explicitly check for the string 'False', which is what a hidden field + # will submit for False (since bool("True") == True we don't need to + # handle that explicitly). + if value == 'False': return False return bool(value) diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py index 3b93d70338..1c436ce680 100644 --- a/tests/regressiontests/forms/fields.py +++ b/tests/regressiontests/forms/fields.py @@ -914,6 +914,11 @@ False >>> f.clean('Django rocks') True +>>> f.clean('True') +True +>>> f.clean('False') +False + >>> f = BooleanField(required=False) >>> f.clean('') False @@ -930,6 +935,11 @@ False >>> f.clean('Django rocks') True +A form's BooleanField with a hidden widget will output the string 'False', so +that should clean to the boolean value False: +>>> f.clean('False') +False + # ChoiceField ################################################################# >>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index ea8cf135aa..0e69602103 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -129,6 +129,13 @@ u'' +Boolean values are rendered to their string forms ("True" and "False"). +>>> w = HiddenInput() +>>> w.render('get_spam', False) +u'' +>>> w.render('get_spam', True) +u'' + # MultipleHiddenInput Widget ################################################## >>> w = MultipleHiddenInput()