Custom field with multiple checkboxes
I'm trying to achieve the attached image with a custom component. Both checkboxes checked means that the student doubled that year.
The data is inside a json field on the record, alongside other json data:
(I know the json format for the groups could be a lot simpler and cleaner and I will probably refactor this)
I've started my custom component, creating the blade view, but I'm a bit lost at how to continue.
- Can/should I use Filament form state to set the checkboxes, and listen to checkbox clicks to change the json accordingly? How? (this sounds ideal because it hooks into default Filament as much as possible)
- If not, I guess I need a method to set the data, which will populate the checkboxes. And "manually" listen to changes, to set the form json?
- Can I use Filament checkboxes inside my custom component?
Solution:Jump to solution
Eventually I found out that I can use
in the custom layout component blade, instead of
@foreach ($getChildComponents() ...
{{ $getChildComponentContainer() }}
....16 Replies
Is the custom field only handling the checkboxes? Where does the rest of the information come from? (school_changes, school_change_reason)
Those would be handled by separate fields.
But... if it's more convenient to separate, I could refactor the schema.
Not sure, probably comes down to personal preference, but personally I like when 1 form field saves to 1 DB column. So if the column is a complex JSON object, I would take all of it into account in my custom field.
Actually I'm working on those other fields. And I found I can work them with
->statePath('json_col')
.
Yea, I would do that too. The data is legacy, the app is new.+ 1 for
statePath()
, it's a good solutionThat works for the other fields, which I can do with standard components. But I still wouldn't know how to "tie things up" with my custom checkboxes component.
Looking at the data format, I'm not really sure how you can map these values cleanly to a set of checkboxes:
I'm guessing the order of the Groups is always fixed somehow?
I can refactor that. Maybe
{0: [1], 1: [1, 2]}
or even [0, 2, 4, 5, 6]
for only the checked indices.
I guess my biggest question is, how can I two-way bind my checkboxes?I think something along those lines would be easier to use with regular Filament checkboxes:
So... build a grid with every checkbox a standard one, the first column having no labels? I thought about that too. I could bind everything with ->statePath
(Untested)
I can try something like that. Still, I would like to understand how custom fields work, state/wire/bind wise. Maybe some plugin that offers a simple custom field to check out...
Do you have access to Laracasts?
This goes quite deep into custom fields and components:
https://laracasts.com/series/build-advanced-components-for-filament
Laracasts
Build Advanced Components for Filament
Once you're confidently building interfaces with all the components that are included with Filament, you'll likely want to begin building your own custom ones.
Hi, I'm Dan: the co-founder of Filament. This series will teach you how to build components, like custom form fields and table columns, and how to distribute them in a plugin package. Me...
Only a free account. But most of that series seems free. Definitely going to watch! Thanks!
extractPublicMethods()
.... I was wondering where that magic came from. And now I know. ^^Solution
Eventually I found out that I can use
in the custom layout component blade, instead of
@foreach ($getChildComponents() ...
{{ $getChildComponentContainer() }}
.
Now I can apply my custom layout/styling "inside"/"between" the default or custom components provided by ->schema([...])
, instead of only "around" the fully rendered set of child components.