Opened 17 years ago

Closed 17 years ago

#3130 closed defect (duplicate)

Fix for BooleanFields in newforms

Reported by: ben <ben.dean.kawamura@…> Owned by: nobody
Component: Forms Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The BooleanField in the newforms library doesn't seem to work when required=True. If the user doesn't check the checkbox, it always fails the validation check. What should happen is the field should have it's value set to False.

Attachments (1)

boolfield.diff (357 bytes ) - added by ben <ben.dean.kawamura@…> 17 years ago.
Here's a quick patch to fix the problem

Download all attachments as: .zip

Change History (9)

by ben <ben.dean.kawamura@…>, 17 years ago

Attachment: boolfield.diff added

Here's a quick patch to fix the problem

comment:1 by Adrian Holovaty, 17 years ago

Resolution: invalid
Status: newclosed

This behavior is intentional. required=True on a checkbox means "the user must check the box."

in reply to:  1 comment:2 by njharman@…, 17 years ago

Replying to adrian:

This behavior is intentional. required=True on a checkbox means "the user must check the box."

That is weird, and far from "least suprise". Booleans have two vaules T/F, not checking a check box should be "False" not "Null"

If I want to accept true/false values in my newforms form I must add 'required=False' even though the field is in fact required and the related model will not accept a Null value.

Very strange.

comment:3 by Chris Beaven, 17 years ago

Resolution: invalid
Status: closedreopened
Triage Stage: UnreviewedDesign decision needed

Yes, this needs some more thought. Avoiding the required=True issue, if required is False then the field should clean to False, not None (easily achieved by moving the new code in this patch below the parent clean() call.

comment:4 by mfenniak, 17 years ago

Just got bit by this unintuitive behaviour, and I thought I would add my input. Fundamentally, I don't see why anyone would want a checkbox that is always checked. That sounds to me like "no option", and not something that belongs on a form. That behaviour for required=True is not intuitive. I suggest that:

If the BooleanField is required=True, that means "the user must select yes or no". This sounds like a pair of radio buttons (or alternatively, a select drop-down), yes and no, where neither is selected. This means that the user cannot avoid selecting an option, or else the form validation fails.

If the BooleanField is required=False, then it can be displayed as a checkbox.

The current behaviour is primarily broken because required=True is the default for all fields on a form. If you add a BooleanField, you now have to check it or else fail validation. There really isn't an option in the field.

comment:5 by James Bennett, 17 years ago

Just wanted to point out that there are use cases for the current behavior; for example, a user-registration form might want to require the the user check an "I agree to terms of service" box before proceeding to account creation. Doesn't necessarily have bearing on what the default behavior should be, but there are definitely cases where the "must be checked" behavior is desirable.

comment:6 by ben, 17 years ago

I think the simplest way to change things is to make BooleanFields required=False by default. If someone wants to have a checkbox you must check to get to the next page, they can set required=True. But that seems like it will definitely be less used.

comment:7 by Marty Alchin <gulopine@…>, 17 years ago

I fought with this particular issue for a while, and I have to agree with ubernostrum and ben. I can definitely understand situations like "I agree to the terms of service" and "I am 30 years old or older" and maybe even "I hereby place this content in the public domain", but I can't imagine these to be the norm.

It does seem the W3C considered this to be the case, however, as their definition of successful controls lists checkboxes only in the "on" position. Naturally, browsers implemented this such that unchecked checkboxes aren't submitted at all, and it seems that Django is simply following suit.

In terms of the early Web, however, an unsubmitted checkbox rarely meant that a form was in error. Instead, it typically indicated that a particular bit of code wouldn't be processed. Throwing an error in this case would seem counter-intuitive to most developers, while those few situations that do in fact require a checked checkbox would easily merit an explicit required=True in the form definition.

comment:8 by James Bennett, 17 years ago

Resolution: duplicate
Status: reopenedclosed

Duplicate of #5104; let's get all this discussion into one place.

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