Default value for multiple select field. But it needs to be hidden

Hi together, I have the models Company and CompanyType which are linked via the pivot model CompanyHasCompanyTypes. So in the EditPage I have set up this SelectField:
Select::make('company_types')
->label(__('filament.company_type'))
->searchable()
->preload()
->required()
->multiple()
->validationMessages([
'required' => __('validation.custom.company_type.required'),
])
->relationship('companyTypes', 'name')
Select::make('company_types')
->label(__('filament.company_type'))
->searchable()
->preload()
->required()
->multiple()
->validationMessages([
'required' => __('validation.custom.company_type.required'),
])
->relationship('companyTypes', 'name')
And it works. I can select the company types and they are saved correctly. Now there is a condition, that depending which user is logged in and depending on the user roles they have, some fields are shown and some are not. CompanyTypes is one of them. What I need to do now, is to still provide a default value without displaying the field. I have tried this first:
Select::make('company_types')
->default([1])
->multiple()
->hidden()
->relationship('companyTypes', 'name'),
Select::make('company_types')
->default([1])
->multiple()
->hidden()
->relationship('companyTypes', 'name'),
but that didn't do it. Is there a possibility to achieve the desired behaviour?
Solution:
Use the mutateDateBeforeCreate()/mutateDateBeforeSave() to fill in the data based on the role instead.
Jump to solution
8 Replies
Solution
Dennis Koch
Dennis Koch4w ago
Use the mutateDateBeforeCreate()/mutateDateBeforeSave() to fill in the data based on the role instead.
sparmin
sparminOP4w ago
Yeah that's my workaround for now too. Just thought there might be a cleaner solution 🙂
Dennis Koch
Dennis Koch4w ago
Nope, that's the best solution
sparmin
sparminOP4w ago
Gotcha. Thank you very much🙏
sparmin
sparminOP4w ago
@Dennis Koch Okay follow up question: for some reason, for the displayed select field, the data is not shown inside $data of mutateFormDataBeforeSave(). Any idea why? It does get saved correctly though
Dennis Koch
Dennis Koch4w ago
It’s because relationships are saved directly. Sorry I missed that. But it shouldn’t matter for the case where you want to pass a default.
sparmin
sparminOP4w ago
Ah I see. Yes it shouldn't matter, was just confused
Dennis Koch
Dennis Koch4w ago
Yeah. That was more reassurance for myself 😅

Did you find this page helpful?