Code

Opened 6 years ago

Closed 6 years ago

#6156 closed (duplicate)

Boolean field should force checking, but it is not.

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

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.

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by sacrebis@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 6 years ago by brosner

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #5957.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.