Skip to content

checkboxes

A group of checkboxes submitted as an array of the checked values.

Lte3::checkboxes(string $name, mixed $selected = null, array $options = [], array $attrs = [])
{!! 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 is a plain PHP array (not a Collection). Each element is either an array or a scalar:

ElementSubmitted valueLabel
Arrayfirst non-empty of id, slug, key, valuefirst of label, name, title
Scalar ('evening', 'day' => 'Day')the element itselfthe element itself

Array elements can also have:

  • disabled — true disables this checkbox.
  • readonly — true makes it unchangeable (onclick="return false;"), but still submitted when checked.
  • attrs — extra HTML attributes of this checkbox, ['data-price' => 10].

The checked values are resolved in the component view, not by the common Value resolution:

  1. $selected, if not null.
  2. Otherwise the form model attribute $model->{$name}, if Lte3::formOpen(['model' => ...]) is open.
  3. Otherwise old($name).
  4. Otherwise the default attr.

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']) !!}
AttrTypeDefaultDescription
labelstring-Group label HTML. Shown only when set.
helpstring-Help HTML under the group.
defaultmixed-Checked values when there is no $selected, model and old input.
disabledsarray[]Option values to disable.
readonlysarray[]Option values to make unchangeable.
unchecked_valuemixed0Value of a hidden input name submitted when nothing is checked. '' removes it.
is_simpleboolfalsePlain Bootstrap form-check markup instead of custom-control.
class_controlstring-Extra classes of each control wrapper, e.g. custom-switch.
classstring-Extra classes of each <input>.
class_wrapstring-Extra classes of the wrapper .form-group, e.g. row.
hidden_wrapboolfalseHides the whole group.
titlestring-Tooltip on the wrapper.
field_id_prefixstring''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.

<input type="hidden" name="cars" value="0">
<input type="checkbox" name="cars[]" value="audi">
<input type="checkbox" name="cars[]" value="tesla">
  • Some checked: cars is an array of the checked values, ['audi', 'tesla'] (the hidden scalar is replaced by the array).
  • Nothing checked: cars is "0" (the unchecked_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'))]);
  • checkbox — a single checkbox, with AJAX save.
  • radiogroup — one value from a group.
  • select2 with multiple — the same choice as a dropdown.