Opened 16 years ago

Closed 16 years ago

#6156 closed (duplicate)

Boolean field should force checking, but it is not.

Reported by: sacrebis@… Owned by: nobody
Component: Uncategorized Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Documentation says:


  • Empty value: False
  • Validates that the check box is checked (i.e. the value is True) if the field has required=True

Note

Since all Field subclasses have required=True by default, the validation condition here is important. If you want to include a checkbox in your form that can be either checked or unchecked, you must remember to pass in required=False when creating the BooleanField.


But:

>>> from django.newforms import *
>>> class F(Form):
>>>    foo = BooleanField(required=True)

>>> f = F(data={}) # AFAIK if checkinput is not check then browser doesn't send anything in data
>>> f.is_valid() # should be False with errors that foo field is required 
True
>>> print f.cleaned_data['foo']
False

checked on version SVN r6670.

Change History (2)

comment:1 by sacrebis@…, 16 years ago

That is the cause of this error:
Documentation says:

  • Empty value: False
  • Normalizes to: A Python True or False value.

but require condition in Field.clean() (called by BooleanField.clean).

class Field(self,...):
   ...
   def clean(self, value):
       ### i got value=False here
       if self.required and value in EMPTY_VALUES:
           ### this will not be raised !!!
           raise ValidationError(self.error_messages['required'])
       return value

and EMPTY_VALUES=(None, )

because we got False from widget CheckboxInput:

class CheckboxInput(Widget):
    ...
    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

comment:2 by Brian Rosner, 16 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #5957.

Note: See TracTickets for help on using tickets.
Back to Top