Checkbox
Basic Usage
The Checkbox field renders a single checkbox input. By default, it stores true when checked and false when unchecked:
use InertiaUI\Forms\Fields\Checkbox;
Checkbox::make('receive_updates')
->label('Send project updates')
->help('Receive a summary when review status changes.');Use Checkbox for a single boolean value. For several related checkbox options, use Checkbox Group. For a choice of one option out of many, use Radio.
Custom Values
To customize the value that is stored when the checkbox is checked or unchecked, you may use the trueValue and falseValue methods.
Boolean (Default)
These are the values the field uses by default:
Checkbox::make('is_active')
->trueValue(true)
->falseValue(false);String Values
Checkbox::make('publish_status')
->label('Publish this entry')
->help('Published entries are visible to external reviewers.')
->trueValue('published')
->falseValue('draft')
->default('published');Integer Values
Checkbox::make('enabled')
->trueValue(1)
->falseValue(0);The stored values may be strings, numbers, booleans, or null. Arrays and objects are rejected during field configuration. On the server, a model or default value that strictly matches either configured value is kept as it is. Any other value is converted using PHP truthiness and then mapped to trueValue or falseValue. Vue and React also use strict equality to decide whether the checkbox is checked.
For example, this field writes null when the checkbox is unchecked:
Checkbox::make('publish_status')
->trueValue('published')
->falseValue(null);Validation
The Checkbox field validates the submitted value against the configured pair. The default boolean values add Laravel's boolean rule. Custom values add a Rule::in() constraint, and a configured null value also adds the nullable rule. Optional fields that are omitted from the request are not validated.
You may replace this managed constraint with your own boolean, in, or Rule::in() rule:
Checkbox::make('role')
->trueValue('admin')
->falseValue('member')
->rule('in:admin,member,guest');The field doesn't add an acceptance rule automatically, so add the Laravel rule that matches the stored values. In particular, the required method only adds Laravel's required rule. To require the checkbox to be checked, add the accepted rule:
Checkbox::make('terms')
->label('I agree to the terms')
->rule('accepted');The required flag is also exposed to the rendered checkbox for accessibility. Package forms rely on server validation and render with native browser validation disabled.
Validation Key
By default, the checkbox shows the validation errors for its own name. To show the errors of another validation key instead, you may use the validationKey method:
Checkbox::make('agree_to_terms')->validationKey('terms');Show / Hide Errors
Validation errors appear below the checkbox by default. To hide them, call the hideErrors method:
Checkbox::make('agree_to_terms')->hideErrors();The showErrors method shows them explicitly, which is the default behavior:
Checkbox::make('agree_to_terms')->showErrors();Using the Component Directly
You may render Checkbox outside the generated <Form> with local state, or pass the serialized form.getField(...) props while rendering forms manually.
<script setup lang="ts">
import { shallowRef } from 'vue'
import { Checkbox } from '@inertiaui/form-vue/components'
type PublishStatus = 'published' | 'draft'
const publishStatus = shallowRef<PublishStatus>('draft')
const trackBlur = () => {}
</script>
<template>
<Checkbox
v-model="publishStatus"
id="publish-status"
name="publish_status"
label="Publish this entry"
help="Published entries are visible to external reviewers."
true-value="published"
false-value="draft"
validation-key="entry.publish_status"
:show-errors="false"
required
precognitive
invalid
error="Choose a publish state."
badge="Workflow"
badge-class="bg-zinc-100 text-zinc-700"
label-trailing="Required"
label-trailing-class="text-zinc-500"
tooltip="Controls external visibility."
help-position="below"
layout="inline"
control-position="start"
label-class="font-medium"
help-class="text-zinc-500"
error-class="text-red-600"
wrapper-class="max-w-xl"
control-class="ring-zinc-300"
class="shadow-sm"
data-testid="publish-status"
aria-label="Publish this entry"
@blur="trackBlur"
/>
</template>import { useState } from 'react'
import { Checkbox } from '@inertiaui/form-react/components'
type PublishStatus = 'published' | 'draft'
export default function PublishStatus() {
const [publishStatus, setPublishStatus] = useState<PublishStatus>('draft')
const trackBlur = () => {}
return (
<Checkbox
value={publishStatus}
onValueChange={(value) => setPublishStatus(value === 'published' ? 'published' : 'draft')}
id="publish-status"
name="publish_status"
label="Publish this entry"
help="Published entries are visible to external reviewers."
trueValue="published"
falseValue="draft"
validationKey="entry.publish_status"
showErrors={false}
required
precognitive
invalid
error="Choose a publish state."
badge="Workflow"
badgeClass="bg-zinc-100 text-zinc-700"
labelTrailing="Required"
labelTrailingClass="text-zinc-500"
tooltip="Controls external visibility."
helpPosition="below"
layout="inline"
controlPosition="start"
labelClass="font-medium"
helpClass="text-zinc-500"
errorClass="text-red-600"
wrapperClass="max-w-xl"
controlClass="ring-zinc-300"
class="shadow-sm"
data-testid="publish-status"
aria-label="Publish this entry"
onBlur={trackBlur}
/>
)
}Vue uses v-model, and React uses a controlled value with onValueChange. Native attributes and listeners target the underlying <input>. See Native Attributes and Events.
The field also inherits the shared Validation, Conditional Visibility, Authorization, Model Binding, Form Class, and Styling APIs for rules, visibility, labels, help text, disabled and required state, layout, and classes.