Skip to content

checkbox

A single checkbox (or switch) with a hidden “unchecked” value, custom checked/unchecked values and optional AJAX save on change.

Lte3::checkbox(string $name, mixed $value = null, array $attrs = [])

The value is resolved as described in Value resolution: old(), request, $value, form model, default. The box is checked when the resolved value is truthy and not identical (!==) to unchecked_value.

{!! Lte3::checkbox('publish', null, ['label' => 'Publish']) !!}
{!! Lte3::checkbox('archived', null, ['label' => 'Archived', 'is_simple' => true]) !!}
{!! Lte3::checkbox('accept', 0, [
'label' => 'Accept <a href="#">Terms</a>',
'checked_value' => 2,
'unchecked_value' => 0,
'class_control' => 'custom-switch',
]) !!}
AttrTypeDefaultDescription
labelstringStr::studly($name)Label HTML (unescaped). '' hides it.
helpstring-Help HTML under the field.
checked_valuemixed1value of the checkbox, submitted when checked.
unchecked_valuemixed0Value of a hidden input with the same name, submitted when unchecked. '' removes the hidden input.
is_simpleboolfalsePlain Bootstrap form-check markup instead of AdminLTE custom-control.
class_controlstring-Extra classes of the control wrapper, e.g. custom-switch for a toggle switch.
classstring-Extra classes of the <input>.
class_wrapstring-Extra classes of the wrapper .form-group.
hidden_wrapboolfalseHides the whole field.
titlestring-Tooltip on the wrapper (also printed on the input, it is a field_attrs key).
disabledboolfalseAdds disabled to the checkbox, see the note below.
readonlyboolfalseAdds onclick="return false;": the state cannot be changed, but the value is still submitted.
idstring_{field_id_prefix}_{name}Input id, also used by the label for.
field_id_prefixstring''Prefix of the generated id. Use it when the same name appears twice on a page (page and modal).
url_savestring-Saves the state by AJAX on change, see AJAX save.
method_savestringPOSTHTTP method of the AJAX request.
formatstringname=valuePayload format of the AJAX request: name=value or name,value.
raw_namestring$name without trailing []Field name sent in the AJAX request.
attrsarray[]Extra HTML attributes of the <input>.

field_attrs keys (required, id, x-model, …) are printed on the <input>.

The component renders a hidden input followed by the checkbox, both with the same name:

<input type="hidden" name="publish" value="0">
<input type="checkbox" name="publish" value="1">
  • Checked: publish=1 (the checkbox comes last and wins).
  • Unchecked: publish=0.
  • With 'unchecked_value' => '': the key is missing when unchecked, as with a plain HTML checkbox.

So $request->boolean('publish') or a boolean validation rule works without extra code.

With url_save, every change sends a request right away, without submitting the form. Used in tables to toggle a flag per row:

{!! Lte3::checkbox('allowed', $user->allowed, [
'label' => 'Allowed',
'url_save' => route('admin.users.update-field', $user),
'method_save' => 'PATCH',
]) !!}

Request (method_save, POST by default, with the X-CSRF-TOKEN header from the layout):

formatBody
name=value (default){raw_name}=1 or {raw_name}=0
name,valuename={raw_name}&value=1 or name={raw_name}&value=0

The AJAX value is always 1/0, not checked_value/unchecked_value.

Expected JSON response:

{"status": "ok", "message": "Saved"}
  • status: "error" — the checkbox goes back to its previous state and message is shown as an error toast.
  • Any other status — message is shown as a success toast.
  • HTTP error — the checkbox goes back to its previous state and a toast is shown.
public function updateField(Request $request, User $user)
{
$user->update($request->only('allowed'));
return response()->json(['status' => 'ok', 'message' => 'Saved']);
}
  • AJAX save is a delegated change handler on .f-checkbox-ajax (the class is added when url_save is set). It works for checkboxes inserted later without any init call. initCheckbox() is a no-op kept for existing data-fn-inits, see JavaScript API.
  • Data attributes on the input: data-url-save, data-method-save, data-format, data-raw-name.
  • checkboxes — a group of checkboxes submitted as an array.
  • tableOptions uses checkbox with custom-switch for its column toggles.