F
Filament2mo ago
Eddy

Can I use custom, non-boolean values for a form toggle?

Is there an easy way to set up a toggle in Filament where the values are custom (e.g., “Value A” and “Value B” as strings) instead of the standard true/false? Or would this require handling state transformations at the resource level? I’d appreciate any advice on the cleanest approach to achieve this.
4 Replies
toeknee
toeknee2mo ago
You can write a custom getter / setter on the model?
Eddy
EddyOP2mo ago
While true, this ‘boolean-to-string’ logic is intended only for this specific form. I believe implementing it at the model level may not be the cleanest approach for my particular scenario.
toeknee
toeknee2mo ago
Then just use ->formatStateUsing() ? so
->formatStateUsing(fn($state) => $state === 'Value A' ? true : false)
->formatStateUsing(fn($state) => $state === 'Value A' ? true : false)
Eddy
EddyOP2mo ago
Thank you @toeknee! For some reason I forgot about it. I also combined it with dehydrateStateUsing for storing the values back as boolean.
Want results from more Discord servers?
Add your server