RelatedCheckBox
Group: Input
Description
RelatedCheckBoxes are checkboxes that belong together: they share
one name, and the values of all the checked boxes arrive at the
route as a single list parameter. Use them when the visitor may
choose any number of options; for a single yes/no answer, use a
plain CheckBox.
Syntax
RelatedCheckBox(name, value)
RelatedCheckBox(name, value, default_value)
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
name |
str |
required | The shared group name, matching one route parameter annotated list[str]. |
value |
str |
required | What this box contributes to the list when checked. |
default_value |
bool |
False |
Whether this box starts checked. |
Like every component, RelatedCheckBox also accepts
styling and attribute keywords. The
HTML attributes it recognizes as keywords are required and
disabled. disabled=True stops the visitor from changing that one
box, and a disabled box adds nothing to the list, even when it is
shown checked. required=True adds the attribute, but Drafter does
not ask the browser to check the form when a button is clicked, so
the route still runs when the box is unchecked. Drafter turns any
other keyword it does not recognize into a CSS style;
Plain HTML attributes
explains that rule.
Examples
from drafter import *
from dataclasses import dataclass
@dataclass
class State:
toppings: list[str]
@route
def index(state: State) -> Page:
return Page(state, [
Header("Sundae Builder"),
"Current toppings:",
BulletedList(state.toppings),
RelatedCheckBox("picks", "sprinkles"),
" sprinkles\n",
RelatedCheckBox("picks", "cherries"),
" cherries\n",
RelatedCheckBox("picks", "regret"),
" regret\n",
Button("Build it", "build")
])
@route
def build(state: State, picks: list[str]) -> Page:
state.toppings = picks
return index(state)
start_server(State([]))
Notes
- The receiving parameter should be annotated
list[str]; with nothing checked it receives an empty list, not an error. - The group is deliberately exempt from the duplicate-name rule, because sharing one name is exactly how the boxes form a group.
- The list records which values were checked, not the order in which the visitor clicked them.
Accessibility
Put each box's describing text directly beside it, as in the example, and introduce the group with a heading or question so its collective purpose is clear.
Related components
- CheckBox: one independent yes/no.
- RadioButtonGroup: choose exactly one instead.