Ticket #16820: checkbox.patch

File checkbox.patch, 2.8 KB (added by Dan Fairs, 13 years ago)

CheckboxInput.value_to_datadict patch with tests

  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 9b95c31..4b629d2 100644
    a b class CheckboxInput(Widget):  
    491491        values =  {'true': True, 'false': False}
    492492        if isinstance(value, basestring):
    493493            value = values.get(value.lower(), value)
    494         return value
     494        return bool(value)
    495495
    496496    def _has_changed(self, initial, data):
    497497        # Sometimes data or initial could be None or u'' which should be the
  • tests/regressiontests/forms/tests/forms.py

    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):  
    17391739
    17401740        form = EventForm()
    17411741        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())
  • tests/regressiontests/forms/tests/widgets.py

    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):  
    232232        self.assertFalse(w._has_changed(True, u'on'))
    233233        self.assertTrue(w._has_changed(True, u''))
    234234
     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
    235245    def test_select(self):
    236246        w = Select()
    237247        self.assertEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """<select name="beatle">
Back to Top