Search code examples
laravel-livewirelaravel-filamentfilamentphp

Laravel filament dynamic select field


i have one question. how do i disable a select column if another prior select or toggle in the same form has been selected as false. To give some context i have an address form and i want to disable the days field on the form when the Status column in the same form has been selected as closed. I've tried many different combinations to make this work but i can't seem to find a solution This is my form

 public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('user_id')
                    ->default(auth()->user()->id)  // set default value to authenticated user ID  // hide if $userId is not null
                    ->relationship('user', 'id')
                    ->visible(false)  // hide in create view
                    ->required(),
                Toggle::make('status')
                    ->default(true)
                    ->live()
                    ->required(),
                Select::make('day')
                    ->enum(Days::class)
                    ->options(Days::class),
                Forms\Components\DatePicker::make('open')
                    ->default(null),
                Forms\Components\DatePicker::make('close')
                    ->default(null),
                Forms\Components\TextInput::make('phone')
                    ->tel()
                    ->maxLength(255),
            ]);
    }

Solution

  • just add below code to your day select input

    use Filament\Forms\Get;
    
    Select::make('day')
        ->enum(Days::class)
        ->options(Days::class)
        ->disabled(fn (Get $get) => !$get('status')),
    

    here link to filament documentation for Disabling a field