Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7753: clean `NullBooleanField` correctly when using `HiddenInp…

…ut`. Thanks to julien and ElliottM.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8661 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b99cc935ebedf52111507ca92641ffde73a3d9ae 1 parent 938f7ea
Jacob Kaplan-Moss authored August 28, 2008
13  django/forms/fields.py
@@ -594,7 +594,18 @@ class NullBooleanField(BooleanField):
594 594
     widget = NullBooleanSelect
595 595
 
596 596
     def clean(self, value):
597  
-        return {True: True, False: False}.get(value, None)
  597
+        """
  598
+        Explicitly checks for the string 'True' and 'False', which is what a
  599
+        hidden field will submit for True and False. Unlike the
  600
+        Booleanfield we also need to check for True, because we are not using
  601
+        the bool() function
  602
+        """
  603
+        if value in (True, 'True'):
  604
+            return True
  605
+        elif value in (False, 'False'):
  606
+            return False
  607
+        else:
  608
+            return None
598 609
 
599 610
 class ChoiceField(Field):
600 611
     widget = Select
14  tests/regressiontests/forms/fields.py
@@ -1091,6 +1091,20 @@
1091 1091
 >>> f.clean('3')
1092 1092
 >>> f.clean('hello')
1093 1093
 
  1094
+# Make sure that the internal value is preserved if using HiddenInput (#7753)
  1095
+>>> class HiddenNullBooleanForm(Form):
  1096
+...     hidden_nullbool1 = NullBooleanField(widget=HiddenInput, initial=True)
  1097
+...     hidden_nullbool2 = NullBooleanField(widget=HiddenInput, initial=False)
  1098
+>>> f = HiddenNullBooleanForm()
  1099
+>>> print f
  1100
+<input type="hidden" name="hidden_nullbool1" value="True" id="id_hidden_nullbool1" /><input type="hidden" name="hidden_nullbool2" value="False" id="id_hidden_nullbool2" />
  1101
+>>> f = HiddenNullBooleanForm({ 'hidden_nullbool1': 'True', 'hidden_nullbool2': 'False' })
  1102
+>>> f.full_clean()
  1103
+>>> f.cleaned_data['hidden_nullbool1']
  1104
+True
  1105
+>>> f.cleaned_data['hidden_nullbool2']
  1106
+False
  1107
+
1094 1108
 # MultipleChoiceField #########################################################
1095 1109
 
1096 1110
 >>> f = MultipleChoiceField(choices=[('1', 'One'), ('2', 'Two')])

0 notes on commit b99cc93

Please sign in to comment.
Something went wrong with that request. Please try again.