You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Filament v2表单单选框控制字段禁用失效问题求助

问题解决:Filament v2中单选框切换时字段禁用逻辑无响应

核心问题

你当前代码里创建了多个同名的Radio::make('radio')组件,每个单选框仅对应一个选项。这种写法会导致Livewire状态管理冲突——所有单选框都绑定到同一个radio字段,但组件结构不符合Radio组的规范,最终造成$get('radio')无法正确获取当前选中值,使得disabled的判断逻辑失效。而hidden能生效是因为它的渲染逻辑不依赖于实时状态的精准同步。

修复方案

把所有单选框合并为一个标准的Radio组,统一管理状态,同时给每个Select字段添加正确的禁用判断:

public static function form(Form $form): Form
{
    return $form
        ->columns(1)->schema(function (RelationManager $livewire) {
            $parameter = $livewire->getOwnerRecord();
            $field = "";
            switch ($parameter->type) {
                case 'I':
                    $field = TextInput::make('value')->required()->mask(
                        fn (TextInput\Mask $mask) => $mask->numeric()->integer(),
                    );
                    break;
                case 'F':
                    $field = TextInput::make('value')->required()->mask(
                        fn (TextInput\Mask $mask) => $mask->numeric()->decimalPlaces(2),
                    );
                    break;
                case 'B':
                    $field = Select::make('value')->required()->translateLabel()->options(["TRUE" => "Verdadero", "FALSE" => "Falso"]);
                    break;
                case 'D':
                    $field = DatePicker::make('value')->translateLabel()->weekStartsOnMonday()->displayFormat('d/m/Y');
                    break;
                case 'T':
                    $field = TimePicker::make('value')->translateLabel()->displayFormat('H:i:s');
                    break;
                default:
                    $field = TextInput::make('value')
                        ->required()
                        ->maxLength(255);
                    break;
            }
            
            // 合并为单个Radio组,统一管理状态
            $radioGroup = Radio::make('radio')
                ->options([
                    'T' => 'Todos',
                    'G' => 'Grupo',
                    'R' => 'Rol',
                    'U' => 'Usuario',
                ])
                ->label('')
                ->default('T')
                ->reactive()
                ->afterStateUpdated(function (callable $set, $state) {
                    // 根据选中状态清空非关联字段
                    $set('group_id', $state !== 'G' ? null : $set('group_id'));
                    $set('rol_id', $state !== 'R' ? null : $set('rol_id'));
                    $set('user_id', $state !== 'U' ? null : $set('user_id'));
                });

            return [
                $radioGroup,
                Grid::make(2)->schema([
                    Select::make('group_id')
                        ->label('')
                        ->options(Group::all()->pluck('name', 'id'))
                        ->searchable()
                        ->disabled(fn (Closure $get) => $get('radio') !== 'G')
                ]),
                Grid::make(2)->schema([
                    Select::make('rol_id')
                        ->label('')
                        ->options(Role::all()->pluck('name', 'id'))
                        ->searchable()
                        ->disabled(fn (Closure $get) => $get('radio') !== 'R')
                ]),
                Grid::make(2)->schema([
                    Select::make('user_id')
                        ->label('')
                        ->options(User::all()->pluck('email', 'id'))
                        ->searchable()
                        ->disabled(fn (Closure $get) => $get('radio') !== 'U')
                ]),
                $field,
            ];
        });
}

额外优化建议

  • 数据库查询(比如Group::all())建议改用query(),避免每次渲染都查询全表,提升性能:
    ->options(Group::query()->pluck('name', 'id'))
    
  • 如果需要保持原布局(单选与Select同行),可以给Radio组添加横向排列样式:
    Radio::make('radio')
        ->options([...])
        ->inline() // 横向展示所有单选选项
        ->label('')
        // 其他配置
    

内容的提问来源于stack exchange,提问作者Hernan Boykier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:07:00