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

Radio

简介

单选框(Radio)提供了一组单选按钮,用以从一组预定义选项中选择单个值:

use Filament\Forms\Components\Radio;

Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published'
])
除了静态值,options() 方法也接受函数静态计算。你可以将各种 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.
Radio

设置选项描述

使用 descriptions() 方法,你可以为每个选项提供描述说明:

use Filament\Forms\Components\Radio;

Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published'
])
->descriptions([
'draft' => 'Is not visible.',
'scheduled' => 'Will be visible.',
'published' => 'Is visible.'
])
除了静态值,descriptions() 方法也接受函数静态计算。你可以将各种 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.
Radio with option descriptions

NOTE

请确保在描述数组中与选项数组中使用的 key 是相同的,这样描述与选项才能匹配正确。

将选项放置在同一行

你可能希望让所有选项显示在同一行,请使用 inline() 方法:

use Filament\Forms\Components\Radio;

Radio::make('feedback')
->label('Like this post?')
->boolean()
->inline()
Inline toggle buttons

或者,你也可以传入一个布尔值,以控制选项是否放置在同一行中:

use Filament\Forms\Components\Radio;

Radio::make('feedback')
->label('Like this post?')
->boolean()
->inline(FeatureFlag::active())
除了静态值,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.

禁用指定选项

你可以使用 disableOptionWhen() 方法禁用指定选项。它接受一个闭包,在该闭包中确定指定值 $value 的选项是否禁用:

use Filament\Forms\Components\Radio;

Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published',
])
->disableOptionWhen(fn (string $value): bool => $value === 'published')
你可以将各种 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.
Option labelstring | Illuminate\Contracts\Support\Htmlable$labelThe label of the option to disable.
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.
Option valuemixed$valueThe value of the option to disable.
Radio with disabled option

如果你想检索未被禁用的选项,比如,用于验证数据,你可以使用 getEnabledOptions()

use Filament\Forms\Components\Radio;

Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published',
])
->disableOptionWhen(fn (string $value): bool => $value === 'published')
->in(fn (Radio $component): array => array_keys($component->getEnabledOptions()))

关于 in() 函数的更多信息,请查阅验证文档

布尔值选项

你可以使用 boolean() 方法,来表示简单的布尔值,如"是"和"否"选项:

use Filament\Forms\Components\Radio;

Radio::make('feedback')
->label('Like this post?')
->boolean()
Boolean radio

要自定义“是”标签,你可以在 boolean() 方法中使用 trueLabel 参数:

use Filament\Forms\Components\Radio;

Radio::make('feedback')
->label('Like this post?')
->boolean(trueLabel: 'Absolutely!')

要自定义“否”标签,你可以在 boolean() 方法中使用 falseLabel 参数:

use Filament\Forms\Components\Radio;

Radio::make('feedback')
->label('Like this post?')
->boolean(falseLabel: 'Not at all!')