createOptionForm - how to import form schema, is it possible?

I'm using a separate form schema to try and cut down on repetition and mistakes, and so far so good it imports and seems to work:
protected function getFormSchema(): array
{
return \App\FormSchemas\UserFormSchema::schema();
}
protected function getFormSchema(): array
{
return \App\FormSchemas\UserFormSchema::schema();
}
As part of that schema I want there to be a createOptionForm on one of the drop down selects that imports the schema from yet another file (
\App\FormSchemas\CategoryFormSchema::schema()
\App\FormSchemas\CategoryFormSchema::schema()
):
Card::make()
->schema([
Forms\Components\Select::make('category_id')
->options(Category::all()->pluck('name', 'id'))
->required()
->createOptionForm([

]),
]),
Card::make()
->schema([
Forms\Components\Select::make('category_id')
->options(Category::all()->pluck('name', 'id'))
->required()
->createOptionForm([

]),
]),
But I'm not sure what I can pass to createOptionForm to make it work, I've tried a bunch of stuff like throwing darts at a board and got nowhere. I've tried:
->createOptionForm(function () {
return \App\FormSchemas\CategoryFormSchema::schema();
}),
->createOptionForm(function () {
return \App\FormSchemas\CategoryFormSchema::schema();
}),
Which doesn't throw an error like other options, but when clicking the button nothing happens... is it not possible to nest these imports?
9 Replies
yagrasdemonde
yagrasdemonde2y ago
Create a public function in your resource then call it with ->createOptionForm(PrimaryResource::getFormGlobalSchema()) for example
cigoler
cigolerOP2y ago
I'm not using the admin portion of the package, but the form builder on its own. But I created a public function on the model itself that will suffice I assume? Which I've done but it's still not doing anything when I click the button. if I ddump: ->createOptionForm(dd(Category::getFormGlobalSchema())), the screenshot is what I get, so it is passing an array with the filament card and two textinputs. So actually I think the problem is nothing to do with this. ...and yeah I've just tried it without any importing and it doesn't work for me. It's not opening the modal for some reason. Hmm. At least I know it's nothing to do with the importing.
Dan Harrin
Dan Harrin2y ago
the problem is that filament doesnt know how to actually create the record so it doesn’t want to waste energy on opening the modal to do nothing you either need to define relationship() or createOptionUsing()
cigoler
cigolerOP2y ago
I'm being gaslighted day to day by AI chat bots and now FIlament!? When will it end. I guess I was so preoccupied with getting the modal to appear I didn't even think that far ahead as to what it was actually going to create. My bad guys, sorry, thanks for the help. Sorry @Dan Harrin , can you advise please. I'm sure I'm doing something super obviously wrong but I'm tearing my hair out (and don't have much left to begin with). I've created a brand new project, installed Jetstream, Filament Admin, Filament Forms, followed the instructions for both, and just for testing purposes created an additional model called Department with a single name field, and linked it to the user model with a BelongsTo: User model:
public function department()
{
return $this->belongsTo(Department::class);
}
public function department()
{
return $this->belongsTo(Department::class);
}
Create.php:
protected function getFormSchema(): array
{
return [
Forms\Components\TextInput::make('name')->required(),
Forms\Components\Select::make('department')
->options(\App\Models\Department::pluck('name', 'id')->toArray())
->relationship('department', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')->required(),

]),
];
}
protected function getFormSchema(): array
{
return [
Forms\Components\TextInput::make('name')->required(),
Forms\Components\Select::make('department')
->options(\App\Models\Department::pluck('name', 'id')->toArray())
->relationship('department', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')->required(),

]),
];
}
That should work shouldn't it? But when I click the button to bring up the modal, a network request is sent to fetch the modal, but nothing else happens and nothing shows. Am I missing something here? Again I don't want to be that guy, so I am sorry.
Dan Harrin
Dan Harrin2y ago
are you missing {{ $this->modal }} in the view, outside of the <form>?
Dan Harrin
Dan Harrin2y ago
cigoler
cigolerOP2y ago
Oh man, I did not, no. I'm shifting uncomfortably in my seat now. Is that documented yet? Either way that gets me to a point where the modal now looks like it's loading the overlay but no form is showing. But I'll take it, that's progress!
Dan Harrin
Dan Harrin2y ago
yes its in the createOptionForm() section any console errors? also, does your Livewire component have just 1 root html element?
cigoler
cigolerOP2y ago
Yeah all fixed - right on both counts. Sorry again Dan, you're a lifesaver.
Want results from more Discord servers?
Add your server