Author: jkocherhans
Date: 2010-02-23 17:37:45 -0600 (Tue, 23 Feb 2010)
New Revision: 12556

Modified:
   django/trunk/django/forms/widgets.py
   django/trunk/tests/regressiontests/forms/forms.py
Log:
Fixed #9336. Changed CheckboxInput to render 'True' and 'False' input strings 
as checked or not instead of as a value attribute. Thanks, bthomas.

Modified: django/trunk/django/forms/widgets.py
===================================================================
--- django/trunk/django/forms/widgets.py        2010-02-23 23:23:38 UTC (rev 
12555)
+++ django/trunk/django/forms/widgets.py        2010-02-23 23:37:45 UTC (rev 
12556)
@@ -382,7 +382,12 @@
             # A missing value means False because HTML form submission does not
             # send results for unselected checkboxes.
             return False
-        return super(CheckboxInput, self).value_from_datadict(data, files, 
name)
+        value = data.get(name)
+        # Translate true and false strings to boolean values.
+        values =  {'true': True, 'false': False}
+        if isinstance(value, basestring):
+            value = values.get(value.lower(), value)
+        return value
 
     def _has_changed(self, initial, data):
         # Sometimes data or initial could be None or u'' which should be the

Modified: django/trunk/tests/regressiontests/forms/forms.py
===================================================================
--- django/trunk/tests/regressiontests/forms/forms.py   2010-02-23 23:23:38 UTC 
(rev 12555)
+++ django/trunk/tests/regressiontests/forms/forms.py   2010-02-23 23:37:45 UTC 
(rev 12556)
@@ -295,6 +295,24 @@
 >>> print f['get_spam']
 <input checked="checked" type="checkbox" name="get_spam" />
 
+'True' or 'true' should be rendered without a value attribute
+>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'True'}, 
auto_id=False)
+>>> print f['get_spam']
+<input checked="checked" type="checkbox" name="get_spam" />
+
+>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'true'}, 
auto_id=False)
+>>> print f['get_spam']
+<input checked="checked" type="checkbox" name="get_spam" />
+
+A value of 'False' or 'false' should be rendered unchecked
+>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'False'}, 
auto_id=False)
+>>> print f['get_spam']
+<input type="checkbox" name="get_spam" />
+
+>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'false'}, 
auto_id=False)
+>>> print f['get_spam']
+<input type="checkbox" name="get_spam" />
+
 Any Field can have a Widget class passed to its constructor:
 >>> class ContactForm(Form):
 ...     subject = CharField()

-- 
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