Calculated State Typed property Filament\Forms\Components\Component::$container must not be accessed

In my UserResource class, I'm attempting to populate a toggle based on the soft delete flag on the user table. I have this in my model:
public function isActive(): bool
{
return is_null($this->deleted_at);
}
public function isActive(): bool
{
return is_null($this->deleted_at);
}
and this in my UserResource form:
Toggle::make('active')->state(function (?User $record): bool {
return $record->isActive();
}),
Toggle::make('active')->state(function (?User $record): bool {
return $record->isActive();
}),
I'm getting the error Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization. Not sure what I'm doing wrong, as doing something similar on another resource worked fine.
2 Replies
cheesegrits
cheesegrits15mo ago
You can't call state() directly like that. You'd need to do something like ...
->afterStateHydrated(function (Toggle $component, bool $state, ?User $record) {
$component->state($record?->isActive() ?? false);
})
->dehydrated(false)
->afterStateHydrated(function (Toggle $component, bool $state, ?User $record) {
$component->state($record?->isActive() ?? false);
})
->dehydrated(false)
And you'll need dehydrated(false) so Filament doesn't try to store anything to your non-existent field. Then of course you'll need to figure out the submission logic.
Jon Mason
Jon MasonOP15mo ago
ok thank you...I opted to just add an is_active flag to the table to simplify things.
Want results from more Discord servers?
Add your server