Default value for checkboxes and toggles
Hey I tried to make the toggle checked by default with this code but it doesn't work. Any idea why?
18 Replies
note that default only works in the 'create' context
Sure. but it isn't checked even in the create page
are you on v2 or v3?
Solution
Uh my bad it is because i am filling the form at somewhere else
deleting this now
don't delete it
Got it
it could help others,
glad you found it though.
Thanks
Since it is related though, i would like to talk about reactive checkboxes and toggles. So I have another field that is only visible when a toggle is checked. This is all fine when creating the record for the first time. Imagine I have created a record and actually enabled the toggle to also fill the field. (Note that i am not saving the enabled toggle field to the database). So next time when I am editting the record, the toggle field should be enabled by default since its related field was filled up. How can i achieve this?
->afterStateHydrated()
you can use that to set the state of other fields
or if you're using the v3 beta https://beta.filamentphp.com/docs/3.x/forms/advanced#field-lifecycle
I am on the v2. Thanks. Which field am i going to use this tho? The toggle field or the field that is only visible when the toggle is on
since it depends on the related field being filled, I would use it on the related field
something like that
obviously you'll need to handle the if check better than that. LOL.
Thank you!
Like how? π It felt completely reasonable to me tbh
i don't know the data for the related field, it could have been a repeater with an array of data. π
could also use
filled($state)
or empty($state)
depending on the type of data.Ok thank you so much for the help