Multi-tenancy and createOptionForm on Select, Field 'company_id' doesn't have a default value

I have a resource that takes Contacts as a Select options. I want to add a Create form to the Select to create a new Contact but when saving get the following error: SQLSTATE[HY000]: General error: 1364 Field 'company_id' doesn't have a default value The ContactResource form works when used standalone from the menu, but does not when used as the createOptionForm
Forms\Components\Select::make('Contact')
->label('Send applications to')
->relationship('contacts', 'name')
->createOptionForm(fn(Form $form) => ContactResource::form($form)),
Forms\Components\Select::make('Contact')
->label('Send applications to')
->relationship('contacts', 'name')
->createOptionForm(fn(Form $form) => ContactResource::form($form)),
// the contact resource form

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('email')
->required(),
Forms\Components\TextInput::make('telephone')
->required(),
Forms\Components\Textarea::make('address')
->required(),
])
->columns(2),
]);
}
// the contact resource form

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\TextInput::make('email')
->required(),
Forms\Components\TextInput::make('telephone')
->required(),
Forms\Components\Textarea::make('address')
->required(),
])
->columns(2),
]);
}
Solution:
Here’s now to do it for anyone finding this: createOptionAction() is where you need to pass in your additional data ```php...
Jump to solution
11 Replies
toeknee
toeknee13mo ago
You need to add company_id to the save method:
mutateFormDataBeforeSave(array $data) : array
{
$data['company_id'] = auth()->user()->company_id;
}
mutateFormDataBeforeSave(array $data) : array
{
$data['company_id'] = auth()->user()->company_id;
}
That would be great
Ric Le Poidevin
Ric Le PoidevinOP13mo ago
I can add that to the Edit page for contacts as in the docs https://filamentphp.com/docs/3.x/panels/resources/editing-records#customizing-data-before-filling-the-form but this isn’t run for the save operation: ->createOptionForm(fn(Form $form) => ContactResource::form($form)), Do I need to put it somewhere else?
Ric Le Poidevin
Ric Le PoidevinOP13mo ago
And if I use mutateFormDataBeforeCreate in the CreateContact this fires on the form’s own page, but not when used with createOptionForm.
toeknee
toeknee13mo ago
Add it to the form there too? form($form)->mutateBeforeCreate
Ric Le Poidevin
Ric Le PoidevinOP13mo ago
No good I’m afraid. I’ve done a composer update and tried mutateFormDataBeforeCreate
No description
toeknee
toeknee13mo ago
Interesting, I would have assumed it would be supported. I am not that familiar with that function, so hopefully source code diving will show you how to handle it, orderwise maybe others can advise. On the save function in the model, just set the default team_id
i really can't play that
what about this? protected function mutateFormDataBeforeCreate(array $data): array { $data['company_id'] = auth()->user()->company_id; return $data; }
Ric Le Poidevin
Ric Le PoidevinOP13mo ago
there’s nowhere that runs on the create option form, it’s only used when creating a standalone form as far as I can see. I shall need to try digging deeper
toeknee
toeknee13mo ago
What about mutateRelationshipDataBeforeCreateUsing? as creationoptionform is for the relationship
Solution
Ric Le Poidevin
Ric Le Poidevin13mo ago
Here’s now to do it for anyone finding this: createOptionAction() is where you need to pass in your additional data
Forms\Components\Select::make('Contact')
->createOptionForm(fn(Form $form) => ContactResource::form($form))
->createOptionAction(fn ($action) => $action->mutateFormDataUsing(function ($data) {
$data['company_id'] = auth()->user()->current_company_id;

return $data;
})),
Forms\Components\Select::make('Contact')
->createOptionForm(fn(Form $form) => ContactResource::form($form))
->createOptionAction(fn ($action) => $action->mutateFormDataUsing(function ($data) {
$data['company_id'] = auth()->user()->current_company_id;

return $data;
})),
Want results from more Discord servers?
Add your server