CheckBox
CheckBox component is part of the Primer ViewComponents forms framework. If you are building a form, please consider using the framework instead of this standalone component.Description
Check boxes are true/false inputs rendered as <input type="checkbox"> in HTML.
Schemes
Check boxes can submit values to the server using one of two schemes, either :array
or :boolean (the default). Check boxes with a scheme of :boolean function like normal
HTML check boxes. If they are checked, a value of "1" is sent to the server; if they are
unchecked, a value of "0" is sent to the server. The checked and unchecked values can be
customized via the :value and :unchecked_value arguments respectively.
Whereas :boolean check boxes must have unique names, :array check boxes all have the
same name. On form submission, Rails will aggregate the values of the check boxes with the
same name and provide them to the controller as an array. If :scheme: is :array, the
:value argument must also be provided. The :unchecked_value argument is ignored. If a
check box is checked on submit, its corresponding value will appear in the array. If it is
not checked, its value will not appear in the array.
Caption templates
Caption templates for :array-type check boxes work a little differently than they do for
other input types. Because the name must be the same for all check boxes that make up an
array, caption template file names are comprised of both the name and the value of each
check box. For example, a check box with the name foo and value bar must have a caption
template named foo_bar_caption.html.erb.
Nested Forms
Check boxes can have "nested" forms that are rendered below the caption. A common use-case is a form that is hidden until the check box is checked. Nested forms are indented slightly to align with the label and caption.
Define a nested form via the #nested_form method, which is expected to return an instance
of a Primer form (see the usage section below).
Any fields defined in the nested form are submitted along with the parent form's fields.
NOTE: Check boxes do not automatically show or hide nested forms. If such behavior is desired, it must be done by hand.
Arguments
| Name | Default | Description |
|---|---|---|
name | StringValue for the HTML name attribute. | |
id | StringValue for the HTML id attribute. | |
class | StringCSS classes to include in the input's HTML | |
classes | ArrayCSS classes to include in the input's HTML | |
caption | StringA string describing the field and what sorts of input it expects. Displayed below the input. | |
label | StringLabel text displayed above the input. | |
visually_hide_label | BooleanWhen set to | |
disabled | BooleanWhen set to | |
hidden | BooleanWhen set to | |
invalid | BooleanIf set to | |
validation_message | StringA string displayed between the caption and the input indicating the input's contents are invalid. This option is, by default, set to the first Rails validation message for the input (assuming the form is associated with a model object). Use | |
label_arguments | HashAttributes that will be passed to Rails' | |
scope_name_to_model | BooleanDefault | |
scope_id_to_model | BooleanDefault | |
required | BooleanDefault | |
aria | HashKey/value pairs that represent Aria attributes and their values. Eg. | |
data | HashKey/value pairs that represent data attributes and their values. Eg. | |
system_arguments | HashA hash of attributes passed to the underlying Rails builder methods. These options may mean something special depending on the type of input, otherwise they are emitted as HTML attributes. See the Rails documentation for more information. In addition, the usual Primer utility arguments are accepted in system arguments. For example, passing | |
name | StringValue for the HTML name attribute. | |
value | StringOn form submission, this value will be sent to the server if the check box is checked. Defaults to "1". | |
unchecked_value | StringOn form submission, this value will be sent to the server if the check box is not checked. Defaults to "0". | |
scheme | SymbolControls how check box values are submitted to the server. One of |