跳到主要内容
版本:4.x

Checkbox

简介

Checkbox 组件,类似于 Toggle,允许你和布尔值互动。

use Filament\Forms\Components\Checkbox;

Checkbox::make('is_admin')
Checkbox

如果你使用 Eloquent 保存该布尔值,请确保该模型属性中添加了 boolean 强制转换(casts)

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
protected $casts = [
'is_admin' => 'boolean',
];

// ...
}

标签置于其上

Checkbox 字段有两个布局模式,行内或者堆叠。默认情况下,使用的是行内模式。

使用行内模式时,标签与之相邻:

use Filament\Forms\Components\Checkbox;

Checkbox::make('is_admin')
->inline()
Checkbox with its label inline

使用堆叠模式时,标签在其上方:

use Filament\Forms\Components\Checkbox;

Checkbox::make('is_admin')
->inline(false)
除了允许静态值之外,inline() 方法也可以接受函数来动态计算它的值。你可以将各种 utility 作为参数注入该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
FieldFilament\Forms\Components\Field$componentThe current field component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current form data. Validation is not run.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Raw statemixed$rawStateThe current value of the field, before state casts were applied. Validation is not run.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the field. Validation is not run.
Checkbox with its label above

Checkbox 验证

除了验证页面中列出的规则之外,还有一些特别针对 Checkbox 字段的其他规则。

Accepted 验证

使用 accepted() 方法,你可以确保 Checkbox 被勾选:

use Filament\Forms\Components\Checkbox;

Checkbox::make('terms_of_service')
->accepted()

此外,你也可以传入一个布尔值,用以控制是否应用该验证规则:

use Filament\Forms\Components\Checkbox;

Checkbox::make('terms_of_service')
->accepted(FeatureFlag::active())
除了允许静态值之外,accepted() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
FieldFilament\Forms\Components\Field$componentThe current field component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current form data. Validation is not run.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Raw statemixed$rawStateThe current value of the field, before state casts were applied. Validation is not run.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the field. Validation is not run.

Declined 验证

使用 declined() 方法,你可以确保 Checkbox 没被勾选:

use Filament\Forms\Components\Checkbox;

Checkbox::make('is_under_18')
->declined()

此外,你也可以传入一个布尔值,用以控制是否应用该验证规则:

use Filament\Forms\Components\Checkbox;

Checkbox::make('is_under_18')
->declined(FeatureFlag::active())
除了允许静态值之外,declined() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
FieldFilament\Forms\Components\Field$componentThe current field component instance.
Get functionFilament\Schemas\Components\Utilities\Get$getA function for retrieving values from the current form data. Validation is not run.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent model FQN?string<Illuminate\Database\Eloquent\Model>$modelThe Eloquent model FQN for the current schema.
Operationstring$operationThe current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>.
Raw statemixed$rawStateThe current value of the field, before state casts were applied. Validation is not run.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current schema.
Statemixed$stateThe current value of the field. Validation is not run.