FilamentF
Filament2y ago
Toni

Empty field on condition

I have the following schema:
                    Toggle::make('notify_unlock')
                        ->label(__('form.labels.notifyUnlock'))
                        ->live()
                        ->columnSpan(1),
                    Select::make('notify_unlock_mail_template_id')
                        ->label(__('form.labels.notifyUnlockMailTemplate'))
                        ->options(MailTemplate::query()->where('is_published','=',1)->pluck('name', 'id'))
                        ->required()
                        ->visible(fn(Get $get) => $get('notify_unlock'))
                        ->columnSpan(2)

I want to empty the select box if the toggle button notify_unlock is false. As far I tried:
  • dehydrateStateUsing
  • beforeStateDehydrated
None of them set the value of the Selectbox to null.

Kind regards,
Toni
Solution
Toggle::make('notify_unlock')
    ->label(__('form.labels.notifyUnlock'))
    ->live()
    ->columnSpan(1)
    ->afterStateUpdated(fn($state, $set) => !$state ? $set('notify_unlock_mail_template_id', null) : '',
Was this page helpful?