diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 9b95c31..4b629d2 100644
a
|
b
|
class CheckboxInput(Widget):
|
491 | 491 | values = {'true': True, 'false': False} |
492 | 492 | if isinstance(value, basestring): |
493 | 493 | value = values.get(value.lower(), value) |
494 | | return value |
| 494 | return bool(value) |
495 | 495 | |
496 | 496 | def _has_changed(self, initial, data): |
497 | 497 | # Sometimes data or initial could be None or u'' which should be the |
diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
index ed783af..887d46e 100644
a
|
b
|
class FormsTestCase(TestCase):
|
1739 | 1739 | |
1740 | 1740 | form = EventForm() |
1741 | 1741 | self.assertEqual(form.as_ul(), u'<input type="hidden" name="happened_at_0" id="id_happened_at_0" /><input type="hidden" name="happened_at_1" id="id_happened_at_1" />') |
| 1742 | |
| 1743 | def test_checkbox_zero(self): |
| 1744 | """ Integration test to demonstrate that '0' is treated as a True |
| 1745 | value when fed through a checkbox input. |
| 1746 | """ |
| 1747 | class FormWithCheckbox(forms.Form): |
| 1748 | flag = BooleanField(widget=CheckboxInput, required=False) |
| 1749 | |
| 1750 | def clean(self): |
| 1751 | cleaned_data = super(FormWithCheckbox, self).clean() |
| 1752 | if cleaned_data.get('flag'): |
| 1753 | raise forms.ValidationError, u'Flag must be false!' |
| 1754 | return cleaned_data |
| 1755 | |
| 1756 | form = FormWithCheckbox({'flag': '0'}) |
| 1757 | |
| 1758 | # The clean method raises a ValidationError if the flag field |
| 1759 | # is True. Since '0' is actually provided, we expect this validation |
| 1760 | # error to be raised, and the form to therefore be not valid. |
| 1761 | self.assertFalse(form.is_valid()) |
diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
index b4ceb2f..79f8f8a 100644
a
|
b
|
class FormsWidgetTestCase(TestCase):
|
232 | 232 | self.assertFalse(w._has_changed(True, u'on')) |
233 | 233 | self.assertTrue(w._has_changed(True, u'')) |
234 | 234 | |
| 235 | def test_checkboxinput_zero(self): |
| 236 | """ CheckboxInput.value_from_datadict should return True when passed |
| 237 | '0', rather than returning the literal '0' which will get treated as |
| 238 | False by BooleanField.to_python(). |
| 239 | """ |
| 240 | w = CheckboxInput() |
| 241 | value = w.value_from_datadict({'flag': '0'}, {}, 'flag') |
| 242 | self.assertTrue(isinstance(value, bool)) |
| 243 | self.assertTrue(value) |
| 244 | |
235 | 245 | def test_select(self): |
236 | 246 | w = Select() |
237 | 247 | self.assertEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """<select name="beatle"> |