Languages

Version

Theme

表单

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)
As well as allowing a static value, the inline() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters. Learn more about utility injection.
Utility Type Parameter Description
Field Filament\Forms\Components\Field $component The current field component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current form data. Validation is not run.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent model FQN ?string<Illuminate\Database\Eloquent\Model> $model The Eloquent model FQN for the current schema.
Operation string $operation The current operation being performed by the schema. Usually create, edit, or view.
Raw state mixed $rawState The current value of the field, before state casts were applied. Validation is not run.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The 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.
Utility Type Parameter Description
Field Filament\Forms\Components\Field $component The current field component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current form data. Validation is not run.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent model FQN ?string<Illuminate\Database\Eloquent\Model> $model The Eloquent model FQN for the current schema.
Operation string $operation The current operation being performed by the schema. Usually create, edit, or view.
Raw state mixed $rawState The current value of the field, before state casts were applied. Validation is not run.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The 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.
Utility Type Parameter Description
Field Filament\Forms\Components\Field $component The current field component instance.
Get function Filament\Schemas\Components\Utilities\Get $get A function for retrieving values from the current form data. Validation is not run.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent model FQN ?string<Illuminate\Database\Eloquent\Model> $model The Eloquent model FQN for the current schema.
Operation string $operation The current operation being performed by the schema. Usually create, edit, or view.
Raw state mixed $rawState The current value of the field, before state casts were applied. Validation is not run.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current schema.
State mixed $state The current value of the field. Validation is not run.
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
Select