F
Filament3d ago
flex

Get Schema from Form for createOptionForm()I

Is it possible to get the schema from an exsiting Resource to pass it to createOptionForm() of another resource without reapting yourself or without chaging anything in the origin resoruce? In Filament 2.0 I think there was getSchema() on Form class. Now it's gone. Is there another option?
10 Replies
Bruno Pereira
Bruno Pereira3d ago
You can use this: https://filamentphp.com/docs/3.x/panels/resources/relation-managers#sharing-a-resources-form-and-table-with-a-relation-manager DOn't mind being in the relation-manager section. Just call the static function form from the resource you want and pass it the $form param
flex
flexOP3d ago
That's what I am doing for relation managers but for createOptionForm it doesn't work for me: Filament\Forms\Components\Select::createOptionForm(): Argument #1 ($schema) must be of type Closure|array|null, Filament\Forms\Form given
flex
flexOP3d ago
No description
Bruno Pereira
Bruno Pereira3d ago
->createOptionForm([ClusterResource::form($form)]) maybe it works :/
toeknee
toeknee2d ago
What I tend to do is put a static function on the resource called ‘formSchema’ which returns an array and just re-use it where I need too.
flex
flexOP2d ago
@toeknee Yes, that would be a wordaround, but would always need to manipulate the origin resource. Unfortunately no.. The error comes a bit later. I can now see and click the plus, but immediately get this one now.. Filament\Forms\ComponentContainer::Filament\Forms\Concerns\{closure}(): Argument #1 ($component) must be of type Filament\Forms\Components\Component, Filament\Forms\Form given
Bruno Pereira
Bruno Pereira2d ago
the InteractWithForms trait has this function getFormSchema() that returns an array. maybe you can play with
->createOptionForm(ClusterResource::form($form)->getFormSchema())
->createOptionForm(ClusterResource::form($form)->getFormSchema())
If that doesnt work, you can go with the function like toeknee said or manually put the form inputs :/
Will 🇬🇹
Will 🇬🇹21h ago
I was running into this earlier today this should work
// option 1
->createOptionForm(fn(Form $form) => YourResource::form($form))

// option 2 if you need to change your form inside the createOptionForm
->createOptionForm(
function (Form $form) {
return YourResource::form($form))
}
)
->createOptionUsing(function (array $data): int {
// logic to create your record
return $newRecord->id
})
->createOptionAction( function (Action $action) {
// if you need to modify the modal
return $action
->modalWidth('md')
->modalHeading('Create modaul');
})
// option 1
->createOptionForm(fn(Form $form) => YourResource::form($form))

// option 2 if you need to change your form inside the createOptionForm
->createOptionForm(
function (Form $form) {
return YourResource::form($form))
}
)
->createOptionUsing(function (array $data): int {
// logic to create your record
return $newRecord->id
})
->createOptionAction( function (Action $action) {
// if you need to modify the modal
return $action
->modalWidth('md')
->modalHeading('Create modaul');
})
toeknee
toeknee12h ago
Well it’s not always? You just need to add a single function to the origin resource? You can’t have that many resources that a simple function is an issue?
flex
flexOP8h ago
Of course I can adjust all the resources I have but doesn't feel as a great approach. It's more a workaround. Sounds good. Will try this later this day.

Did you find this page helpful?