Fixed #5959 -- Fixed handling of False values in hidden boolean fields. Thanks,

SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6745 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-11-29 19:22:03 +00:00
parent 4cbc8c62cf
commit 7a166f1a1c
3 changed files with 22 additions and 4 deletions

View File

@ -536,11 +536,12 @@ class BooleanField(Field):
widget = CheckboxInput widget = CheckboxInput
def clean(self, value): def clean(self, value):
"Returns a Python boolean object." """Returns a Python boolean object."""
super(BooleanField, self).clean(value) super(BooleanField, self).clean(value)
# Explicitly check for the string '0', which is what as hidden field # Explicitly check for the string 'False', which is what a hidden field
# will submit for False. # will submit for False (since bool("True") == True we don't need to
if value == '0': # handle that explicitly).
if value == 'False':
return False return False
return bool(value) return bool(value)

View File

@ -914,6 +914,11 @@ False
>>> f.clean('Django rocks') >>> f.clean('Django rocks')
True True
>>> f.clean('True')
True
>>> f.clean('False')
False
>>> f = BooleanField(required=False) >>> f = BooleanField(required=False)
>>> f.clean('') >>> f.clean('')
False False
@ -930,6 +935,11 @@ False
>>> f.clean('Django rocks') >>> f.clean('Django rocks')
True 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 ################################################################# # ChoiceField #################################################################
>>> f = ChoiceField(choices=[('1', '1'), ('2', '2')]) >>> f = ChoiceField(choices=[('1', '1'), ('2', '2')])

View File

@ -129,6 +129,13 @@ u'<input type="hidden" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u
>>> w.render('email', '', attrs={'class': 'special'}) >>> w.render('email', '', attrs={'class': 'special'})
u'<input type="hidden" class="special" name="email" />' 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 ################################################## # MultipleHiddenInput Widget ##################################################
>>> w = MultipleHiddenInput() >>> w = MultipleHiddenInput()