Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed newforms.CheckboxInput widget to return False as its value wh…

…en not

include in the form (since HTML form submission doesn't send unselected check
boxes). Patch from SmileyChris. Refs #5104.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6563 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e38d54e19a219efedb1b466d77d917bdbb3445f6 1 parent 22ee689
Malcolm Tredinnick malcolmt authored
7 django/newforms/widgets.py
View
@@ -170,6 +170,13 @@ def render(self, name, value, attrs=None):
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)
6 tests/regressiontests/forms/widgets.py
View
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.