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 | Required | Description |
---|---|---|
name | String Value for the HTML name attribute. | |
id | String Value for the HTML id attribute. | |
class | String CSS classes to include in the input's HTML | |
classes | Array CSS classes to include in the input's HTML | |
caption | String A string describing the field and what sorts of input it expects. Displayed below the input. | |
label | String Label text displayed above the input. | |
visually_hide_label | Boolean When set to | |
disabled | Boolean When set to | |
hidden | Boolean When set to | |
invalid | Boolean If set to | |
validation_message | String A 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 | Hash Attributes that will be passed to Rails' | |
scope_name_to_model | Boolean Default | |
scope_id_to_model | Boolean Default | |
required | Boolean Default | |
aria | Hash Key/value pairs that represent Aria attributes and their values. Eg. | |
data | Hash Key/value pairs that represent data attributes and their values. Eg. | |
system_arguments | Hash A 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 | String Value for the HTML name attribute. | |
value | String On form submission, this value will be sent to the server if the check box is checked. Defaults to "1". | |
unchecked_value | String On form submission, this value will be sent to the server if the check box is not checked. Defaults to "0". | |
scheme | Symbol Controls how check box values are submitted to the server. One of |