表单
Toggle
简介
Toggle 组件(切换按钮)类似于 checkbox,允许你与布尔值交互。8
use Filament\Forms\Components\Toggle;
Toggle::make('is_admin')

如果你使用 Eloquent 保存布尔值,你应该确保将 boolean
强制转换(cast)添加到模型属性中:
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $casts = [
'is_admin' => 'boolean',
];
// ...
}
添加图标到切换按钮
Toggle 也可以使用图标来表示按钮的“打开(On)”和“关闭(Off)”状态。要添加图标到“打开”状态,请使用 onIcon()
方法。要添加图标到“关闭态,请使用 offIcon()
方法。
use Filament\Forms\Components\Toggle;
use Filament\Support\Icons\Heroicon;
Toggle::make('is_admin')
->onIcon(Heroicon::Bolt)
->offIcon(Heroicon::User)
除了允许静态值之外,onIcon()
和 offIcon()
方法也接受函数来动态计算他们的值。你可以将多个 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. |

自定义切换按钮颜色
你也可以自定义颜色来表示切换按钮的“打开(On)”和“关闭(Off)”状态。要添加颜色到“打开”状态,请使用 onColor()
方法。要添加颜色到“关闭态,请使用 offColor()
方法。
use Filament\Forms\Components\Toggle;
Toggle::make('is_admin')
->onColor('success')
->offColor('danger')
除了允许静态值之外,onColor()
和 offColor()
方法也接受函数来动态计算他们的值。你可以将多个 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. |


将标签放置在上方
Toggle 字段有两个布局模式,内联(inline)和堆叠(stack)。默认为内联。
内联模式下,其标签在与其相邻:
use Filament\Forms\Components\Toggle;
Toggle::make('is_admin')
->inline()

当布局模式为堆叠时,其标签在按钮上方。
use Filament\Forms\Components\Toggle;
Toggle::make('is_admin')
->inline(false)
除了接收静态值外,该 inline()
方法也接受一个函数进行动态计算。你可以将多个 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. |

Toggle 验证
除了验证页面中列出的所有规则之外,还有其他一些规则专用于 Toggle。
Accepted 验证
使用 accepted()
方法,你可以确保 Toggle 是处于“打开(on)”状态:
use Filament\Forms\Components\Toggle;
Toggle::make('terms_of_service')
->accepted()
同时,你也可以传入一个布尔值,以控制该验证规则是否该适用:
use Filament\Forms\Components\Toggle;
Toggle::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()
方法,你可以确保 Toggle 是处于“关闭(off)”状态:
use Filament\Forms\Components\Toggle;
Toggle::make('is_under_18')
->declined()
同时,你也可以传入一个布尔值,以控制该验证规则是否该适用:
use Filament\Forms\Components\Toggle;
Toggle::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. |
Still need help? Join our Discord community or open a GitHub discussion