summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-08-28 15:06:18 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-08-28 15:06:18 +0000
commitb99cc935ebedf52111507ca92641ffde73a3d9ae (patch)
treea81d4cf473d794e3d91335e48ee713172354464d
parent938f7ea9132d4982bed68fc9f189cea437b8601e (diff)
Fixed #7753: clean `NullBooleanField` correctly when using `HiddenInput`. Thanks to julien and ElliottM.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8661 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/fields.py13
-rw-r--r--tests/regressiontests/forms/fields.py14
2 files changed, 26 insertions, 1 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index afbf6146b0..b582659099 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -594,7 +594,18 @@ class NullBooleanField(BooleanField):
widget = NullBooleanSelect
def clean(self, value):
- return {True: True, False: False}.get(value, None)
+ """
+ Explicitly checks for the string 'True' and 'False', which is what a
+ hidden field will submit for True and False. Unlike the
+ Booleanfield we also need to check for True, because we are not using
+ the bool() function
+ """
+ if value in (True, 'True'):
+ return True
+ elif value in (False, 'False'):
+ return False
+ else:
+ return None
class ChoiceField(Field):
widget = Select
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
index ab7968f4a4..cbd59a4089 100644
--- a/tests/regressiontests/forms/fields.py
+++ b/tests/regressiontests/forms/fields.py
@@ -1091,6 +1091,20 @@ False
>>> f.clean('3')
>>> f.clean('hello')
+# Make sure that the internal value is preserved if using HiddenInput (#7753)
+>>> class HiddenNullBooleanForm(Form):
+... hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True)
+... hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False)
+>>> f = HiddenNullBooleanForm()
+>>> print f
+<input type="hidden" name="hidden_nullbool1" value="True" id="id_hidden_nullbool1" /><input type="hidden" name="hidden_nullbool2" value="False" id="id_hidden_nullbool2" />
+>>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' })
+>>> f.full_clean()
+>>> f.cleaned_data['hidden_nullbool1']
+True
+>>> f.cleaned_data['hidden_nullbool2']
+False
+
# MultipleChoiceField #########################################################
>>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])