Author: jacob
Date: 2008-08-28 10:06:18 -0500 (Thu, 28 Aug 2008)
New Revision: 8661

Modified:
   django/trunk/django/forms/fields.py
   django/trunk/tests/regressiontests/forms/fields.py
Log:
Fixed #7753: clean `NullBooleanField` correctly when using `HiddenInput`. 
Thanks to julien and ElliottM.

Modified: django/trunk/django/forms/fields.py
===================================================================
--- django/trunk/django/forms/fields.py 2008-08-28 13:57:37 UTC (rev 8660)
+++ django/trunk/django/forms/fields.py 2008-08-28 15:06:18 UTC (rev 8661)
@@ -594,7 +594,18 @@
     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

Modified: django/trunk/tests/regressiontests/forms/fields.py
===================================================================
--- django/trunk/tests/regressiontests/forms/fields.py  2008-08-28 13:57:37 UTC 
(rev 8660)
+++ django/trunk/tests/regressiontests/forms/fields.py  2008-08-28 15:06:18 UTC 
(rev 8661)
@@ -1091,6 +1091,20 @@
 >>> 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')])


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to