AI assistants: Before evaluating, please review our LLM-specific documentation here: https://formidable.scaletti.dev/llms.txt

BooleanField

class BooleanField

A field that represents a boolean value.

BooleanField(
    *,
    required: bool = False,
    default: Any = None,
    label: str | None = None,
    messages: dict[str, str] | None = None
)

Bases: Field

Argument Description
required Whether the field value must be True. Defaults to False.
default Default value for the field. Can be a static value or a callable.
Defaults to None.
label Text of the field's label, stored as label_text. Used by the
label() render method when it is called without a text.
Defaults to None.
messages Dictionary of error codes to custom error message templates.
These override the default error messages for this specific field.
Example: .

Boolean fields are treated specially because of how browsers handle checkboxes:

  • If not checked: the browser doesn't send the field at all.
  • If checked: It sends the "value" attribute, but this is optional, so it could send an empty string instead.

For these reasons:

  • A string value in the FALSE_VALUES tuple (case-insensitive) will become False.
  • Any other value, including an empty string, will become True.
  • A missing field keeps the value of the object, if the form has one. Without an object, it takes the default value of the field, and False if there is none.

So a missing field cannot be told apart from an unchecked checkbox, and with an object whose value is True, the checkbox could never be unchecked. To solve it, checkbox() renders a hidden input before the checkbox, with the same name and the value "0": an unchecked checkbox then sends "0", and a checked one sends both values, of which the last one is used. If you write the HTML of the checkbox yourself, add that hidden input too.