Pseude Toggle for activating an other field
I want to use a Toggle Field. The field has no represantation in the database. I just want to use it to activate (show/hide) an other field.
The other field I activate/hide with this:
->hidden(fn (Forms\Get $get): bool => ! $get('with_uploads'))
This works.
But I cant manage it to set the toggles state on form load to the right value, it is always off.
Forms\Components\Toggle::make('with_uploads')
->default(true)
->live(),
Even when I try to use ->state()
with a Function I get the error:
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
Any suggestions?Solution:Jump to solution
`Forms\Components\Toggle::make('with_uploads')
->live()
->afterStateHydrated(function (Announcement $announcement, Forms\Components\Toggle $component) {
if($announcement->uploads()->count()) {
$component->state(true);...
1 Reply
Solution
Forms\Components\Toggle::make('with_uploads')
->live()
->afterStateHydrated(function (Announcement $announcement, Forms\Components\Toggle $component) {
if($announcement->uploads()->count()) {
$component->state(true);
} else {
$component->state(false);
}
}),
This works for me now. Thx.