单选框
简介
单选输入提供一组单选按钮,用于从预定义选项列表中选择单个值:
php
use Filament\Forms\Components\Radio;
Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published'
])TIP
除了允许静态数组外,options() 方法也接受函数以动态计算。你可以将各种工具作为参数注入到该函数中。


设置选项描述
你可以使用 descriptions() 方法为每个选项可选地提供描述:
php
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.'
])TIP
除了允许静态数组外,descriptions() 方法也接受函数以动态计算。你可以将各种工具作为参数注入到该函数中。


INFO
请确保描述数组中的 key 与选项数组中的 key 相同,以便正确的描述对应正确的选项。
将选项彼此行内排列
你可能希望使用 inline() 将选项彼此行内显示:
php
use Filament\Forms\Components\Radio;
Radio::make('feedback')
->label('Like this post?')
->boolean()
->inline()

可选地,你可以传入布尔值来控制选项是否行内排列:
php
use Filament\Forms\Components\Radio;
Radio::make('feedback')
->label('Like this post?')
->boolean()
->inline(FeatureFlag::active())TIP
除了允许静态值外,inline() 方法也接受函数以动态计算。你可以将各种工具作为参数注入到该函数中。
禁用特定选项
你可以使用 disableOptionWhen() 方法禁用特定选项。它接受一个闭包,你可以在其中检查具有特定 $value 的选项是否应被禁用:
php
use Filament\Forms\Components\Radio;
Radio::make('status')
->options([
'draft' => 'Draft',
'scheduled' => 'Scheduled',
'published' => 'Published',
])
->disableOptionWhen(fn (string $value): bool => $value === 'published')TIP
你可以将各种工具作为参数注入到该函数中。


若要获取未被禁用的选项(例如用于校验),可以使用 getEnabledOptions():
php
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() 方法:
php
use Filament\Forms\Components\Radio;
Radio::make('feedback')
->label('Like this post?')
->boolean()

要自定义「是」标签,可以在 boolean() 方法上使用 trueLabel 参数:
php
use Filament\Forms\Components\Radio;
Radio::make('feedback')
->label('Like this post?')
->boolean(trueLabel: 'Absolutely!')要自定义「否」标签,可以在 boolean() 方法上使用 falseLabel 参数:
php
use Filament\Forms\Components\Radio;
Radio::make('feedback')
->label('Like this post?')
->boolean(falseLabel: 'Not at all!')
