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:Jump to 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....4 Replies
Do you want to work with
hidden/visible in your codebase too? Or do you want true/false in your codebase?
Probably the first.@Dennis Koch I would say hidden / visible in the codebase to align to the db structure as much as possible.
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.Thanks, will experiment today
I decided to just change it to a drop down and not bother faffing for this release. Thank you.