checkboxes
A group of checkboxes submitted as an array of the checked values.
Signature
Section titled “Signature”Lte3::checkboxes(string $name, mixed $selected = null, array $options = [], array $attrs = [])Basic example
Section titled “Basic example”{!! Lte3::checkboxes('cars', ['audi', 'tesla'], [ ['id' => 'audi', 'name' => 'Audi'], ['id' => 'tesla', 'name' => 'Tesla'], ['id' => 'bmw', 'name' => 'BMW'], ['id' => 'ford', 'name' => 'Ford', 'disabled' => true],], [ 'label' => 'Cars', 'help' => '* Pick any',]) !!}Options
Section titled “Options”$options is a plain PHP array (not a Collection). Each element is either an array or a scalar:
| Element | Submitted value | Label |
|---|---|---|
| Array | first non-empty of id, slug, key, value | first of label, name, title |
Scalar ('evening', 'day' => 'Day') | the element itself | the element itself |
Array elements can also have:
disabled—truedisables this checkbox.readonly—truemakes it unchangeable (onclick="return false;"), but still submitted when checked.attrs— extra HTML attributes of this checkbox,['data-price' => 10].
Selected values
Section titled “Selected values”The checked values are resolved in the component view, not by the common Value resolution:
$selected, if notnull.- Otherwise the form model attribute
$model->{$name}, ifLte3::formOpen(['model' => ...])is open. - Otherwise
old($name). - Otherwise the
defaultattr.
The result is wrapped into an array, and the old($name) values are always added to it. A value is checked when it is in this array (loose in_array).
request() and getFormValue() are not used. The model lookup reads the attribute by the literal field name, so for relations or nested names pass $selected explicitly:
{!! Lte3::checkboxes('roles', $user->roles->pluck('id')->all(), $roleOptions, ['label' => 'Roles']) !!}| Attr | Type | Default | Description |
|---|---|---|---|
label | string | - | Group label HTML. Shown only when set. |
help | string | - | Help HTML under the group. |
default | mixed | - | Checked values when there is no $selected, model and old input. |
disableds | array | [] | Option values to disable. |
readonlys | array | [] | Option values to make unchangeable. |
unchecked_value | mixed | 0 | Value of a hidden input name submitted when nothing is checked. '' removes it. |
is_simple | bool | false | Plain Bootstrap form-check markup instead of custom-control. |
class_control | string | - | Extra classes of each control wrapper, e.g. custom-switch. |
class | string | - | Extra classes of each <input>. |
class_wrap | string | - | Extra classes of the wrapper .form-group, e.g. row. |
hidden_wrap | bool | false | Hides the whole group. |
title | string | - | Tooltip on the wrapper. |
field_id_prefix | string | '' | Prefix of the generated ids _{prefix}_{name}_{index}. Set it when the same name appears twice on a page. |
field_attrs keys (required, x-model, …) and the attrs attr are not printed by this component; use the per-option attrs instead.
What is submitted
Section titled “What is submitted”<input type="hidden" name="cars" value="0"><input type="checkbox" name="cars[]" value="audi"><input type="checkbox" name="cars[]" value="tesla">- Some checked:
carsis an array of the checked values,['audi', 'tesla'](the hidden scalar is replaced by the array). - Nothing checked:
carsis"0"(theunchecked_value), or missing with'unchecked_value' => ''.
Validate with this in mind, e.g. normalize before validation:
$request->merge(['cars' => array_filter((array) $request->input('cars'))]);Related
Section titled “Related”- checkbox — a single checkbox, with AJAX save.
- radiogroup — one value from a group.
- select2 with
multiple— the same choice as a dropdown.