Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #5959 -- Fixed handling of False values in hidden boolean field…

…s. Thanks,

SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6745 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7a166f1a1c126bea547ad00387ff134ee4743b1c 1 parent 4cbc8c6
Malcolm Tredinnick malcolmt authored
9 django/newforms/fields.py
View
@@ -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)
10 tests/regressiontests/forms/fields.py
View
@@ -914,6 +914,11 @@
>>> f.clean('Django rocks')
True
+>>> f.clean('True')
+True
+>>> f.clean('False')
+False
+
>>> f = BooleanField(required=False)
>>> f.clean('')
False
@@ -930,6 +935,11 @@
>>> 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')])
7 tests/regressiontests/forms/widgets.py
View
@@ -129,6 +129,13 @@
>>> w.render('email', '', attrs={'class': 'special'})
u'<input type="hidden" class="special" name="email" />'
+Boolean values are rendered to their string forms ("True" and "False").
+>>> w = HiddenInput()
+>>> w.render('get_spam', False)
+u'<input type="hidden" name="get_spam" value="False" />'
+>>> w.render('get_spam', True)
+u'<input type="hidden" name="get_spam" value="True" />'
+
# MultipleHiddenInput Widget ##################################################
>>> w = MultipleHiddenInput()
Please sign in to comment.
Something went wrong with that request. Please try again.