Author: mtredinnick
Date: 2007-10-20 07:21:07 -0500 (Sat, 20 Oct 2007)
New Revision: 6563
Modified:
django/trunk/django/newforms/widgets.py
django/trunk/tests/regressiontests/forms/widgets.py
Log:
Changed newforms.CheckboxInput widget to return False as its value when not
include in the form (since HTML form submission doesn't send unselected check
boxes). Patch from SmileyChris. Refs #5104.
Modified: django/trunk/django/newforms/widgets.py
===================================================================
--- django/trunk/django/newforms/widgets.py 2007-10-20 11:05:15 UTC (rev
6562)
+++ django/trunk/django/newforms/widgets.py 2007-10-20 12:21:07 UTC (rev
6563)
@@ -170,6 +170,13 @@
final_attrs['value'] = force_unicode(value) # Only add the 'value'
attribute if a value is non-empty.
return u'<input%s />' % flatatt(final_attrs)
+ def value_from_datadict(self, data, files, name):
+ if name not in data:
+ # 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)
+
class Select(Widget):
def __init__(self, attrs=None, choices=()):
super(Select, self).__init__(attrs)
Modified: django/trunk/tests/regressiontests/forms/widgets.py
===================================================================
--- django/trunk/tests/regressiontests/forms/widgets.py 2007-10-20 11:05:15 UTC
(rev 6562)
+++ django/trunk/tests/regressiontests/forms/widgets.py 2007-10-20 12:21:07 UTC
(rev 6563)
@@ -276,6 +276,12 @@
>>> w.render('greeting', None)
u'<input type="checkbox" name="greeting" />'
+The CheckboxInput widget will return False if the key is not found in the data
+dictionary (because HTML form submission doesn't send any result for unchecked
+checkboxes).
+>>> w.value_from_datadict({}, {}, 'testing')
+False
+
# Select Widget ###############################################################
>>> w = Select()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---