F
Filament2mo ago
keiron

Boolean form field that saves as 'visible' or 'hidden' in the database

I have a database column which has the value 'visible' or 'hidden'. I want to display this as a checkbox which when ticked, saves as 'visible' and when unticked saves as 'hidden'. I have the following code: Group::make() ->relationship('profile') ->schema([ Checkbox::make('visibility_status') ->label('Visible'), ]), When I save the form without interacting with this field, I get the error: The visible field must be true or false. Can someone please advise on the best way to implement this? Thanks.
Solution:
I guess the easiest way is a Radio field. If you want the checkbox, you need to change the state via the mutation callbacks (like mutateFormDataBefore). You could also try something with ->state() and ->afterStateUpdated() diretly on the Checkbox....
Jump to solution
4 Replies
Dennis Koch
Dennis Koch2mo ago
Do you want to work with hidden/visible in your codebase too? Or do you want true/false in your codebase? Probably the first.
keiron
keironOP2mo ago
@Dennis Koch I would say hidden / visible in the codebase to align to the db structure as much as possible.
Solution
Dennis Koch
Dennis Koch2mo ago
I guess the easiest way is a Radio field. If you want the checkbox, you need to change the state via the mutation callbacks (like mutateFormDataBefore). You could also try something with ->state() and ->afterStateUpdated() diretly on the Checkbox.
keiron
keironOP2mo ago
Thanks, will experiment today I decided to just change it to a drop down and not bother faffing for this release. Thank you.

Did you find this page helpful?