Ticket #5104: checkboxinput.2.patch

File checkboxinput.2.patch, 2.6 KB (added by Chris Beaven, 17 years ago)
  • django/newforms/widgets.py

     
    153153            final_attrs['value'] = force_unicode(value) # Only add the 'value' attribute if a value is non-empty.
    154154        return u'<input%s />' % flatatt(final_attrs)
    155155
     156    def value_from_datadict(self, data, name):
     157        if name not in data:
     158            # A missing value returns False because it simply means the check
     159            # box wasn't checked.
     160            return False
     161        return super(CheckboxInput, self).value_from_datadict(data, name)
     162
    156163class Select(Widget):
    157164    def __init__(self, attrs=None, choices=()):
    158165        self.attrs = attrs or {}
  • docs/newforms.txt

     
    10151015~~~~~~~~~~~~~~~~
    10161016
    10171017    * Default widget: ``CheckboxInput``
    1018     * Empty value: ``None``
     1018    * Empty value: ``False``
    10191019    * Normalizes to: A Python ``True`` or ``False`` value.
    1020     * Validates nothing (i.e., it never raises a ``ValidationError``).
    10211020
    10221021``CharField``
    10231022~~~~~~~~~~~~~
     
    10251024    * Default widget: ``TextInput``
    10261025    * Empty value: ``''`` (an empty string)
    10271026    * Normalizes to: A Unicode object.
    1028     * Validates nothing, unless ``max_length`` or ``min_length`` is provided.
     1027    * Validates ``max_length`` and ``min_length`` if they are provided.
    10291028
    10301029Has two optional arguments for validation, ``max_length`` and ``min_length``.
    10311030If provided, these arguments ensure that the string is at most or at least the
     
    11261125    * Default widget: ``NullBooleanSelect``
    11271126    * Empty value: ``None``
    11281127    * Normalizes to: A Python ``True``, ``False`` or ``None`` value.
    1129     * Validates nothing (i.e., it never raises a ``ValidationError``).
    11301128
    11311129``RegexField``
    11321130~~~~~~~~~~~~~~
  • tests/regressiontests/forms/tests.py

     
    276276>>> w.render('greeting', None)
    277277u'<input type="checkbox" name="greeting" />'
    278278
     279Note that the CheckboxInput widget will return False if the key is not found in
     280the data dict.
     281>>> print w.value_from_datadict({'testing':None}, 'testing')
     282None
     283>>> print w.value_from_datadict({}, 'testing')
     284False
     285
    279286# Select Widget ###############################################################
    280287
    281288>>> w = Select()
Back to Top