form->fill()`with Selects?

Anyone know how to use $this->form->fill( [...] ) with Select fields? I need to automatically select something from the dropdown while inside a form action function.
52 Replies
awcodes
awcodes2w ago
Guess it depends on the select and what the available options are.
bionary
bionaryOP2w ago
My Select looks like this:
Select::make('parent_id')->hint('Indicates: /parent/nested/path')
->relationship(
name: 'parent',
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('project_id', $get('project_id')),
)
->getOptionLabelFromRecordUsing(fn (Category $record) => $record->ancestor_path),
Select::make('parent_id')->hint('Indicates: /parent/nested/path')
->relationship(
name: 'parent',
modifyQueryUsing: fn (Get $get, Builder $query) => $query->where('project_id', $get('project_id')),
)
->getOptionLabelFromRecordUsing(fn (Category $record) => $record->ancestor_path),
I thought I could assign the id like:
$this->form->fill([
'parent_id' => 100,
]);
$this->form->fill([
'parent_id' => 100,
]);
awcodes
awcodes2w ago
Ie, you can’t set a select with a preload if the the form fill doesn’t include the the key for the select. But shouldn’t relally be a problem unless the select is a relationship Ok, so you’re assuming that the select options has a key, which It might not have. So it will default to null.
bionary
bionaryOP2w ago
let me look at the hmtl output in console
awcodes
awcodes2w ago
If the select is a relationship then that relationship might not exist so you have to handle that.
bionary
bionaryOP2w ago
Yes, I have a relationship there. In google console, I have many parent_id's there.
I tried hardcoding in a value that I can see it there in google console: like this:
$this->form->fill([
'parent_id' => 329 ,
]);
$this->form->fill([
'parent_id' => 329 ,
]);
But it does not select that item. Maybe the form is reloading at that instance. I've been duped before by the constant livewire magic.
awcodes
awcodes2w ago
Right but a relationship select is async so loading in a id of 100 etc doesn’t guarantee that it’s even an option in the select. And without preload() there are 0 options in the select.
bionary
bionaryOP2w ago
yes, that is what I am thinking.
awcodes
awcodes2w ago
Basically you are saying that the default should be 100 for the key, but you aren’t guaranteeing that 100 is even a key in the options for the select.
bionary
bionaryOP2w ago
yes
awcodes
awcodes2w ago
If that makes sense.
bionary
bionaryOP2w ago
it does
awcodes
awcodes2w ago
So just make the select preload with that key and it should be fine.
bionary
bionaryOP2w ago
I thought I could programmatically select the value using Set $set but that is not working in this context
awcodes
awcodes2w ago
Right, because it’s a relationship. You can’t tell it to be a default before retrieving a data set. Which is where preload comes into play.
bionary
bionaryOP2w ago
I tried preload()... no good. I think perhaps the problem is because I am calling $this->form->fill() from outside the resource? I am calling it from inside the ->action() of a button/form/modal inside getHeaderActions()
awcodes
awcodes2w ago
Preload can define a query on the relationship that includes the defaults.
bionary
bionaryOP2w ago
The rest of the form fields (text input) fill in. okay, let me look at the preload docs
awcodes
awcodes2w ago
Maybe you need to access the livewire instance in the action to get the relationship. Hard to say without seeing the actual code.
bionary
bionaryOP2w ago
I do appreciate the help Adam, thank you.
awcodes
awcodes2w ago
Just remember that you can always inject the livewire component and get the record from there. Even if if feels gross. We don’t get paid to be pretty. We get paid to make it work.
bionary
bionaryOP2w ago
lol, I was just looking at the livewire instance and "gross" was EXACTLY the word I had in mind! By inject do you mean something like this:
$livewire->data['parent_id'] = 329;
awcodes
awcodes2w ago
I was referring to DI, like in the select you can do something like fn($record) => doSomethingwith($record)
bionary
bionaryOP2w ago
It seems to be getting messed up from the relationship on the Select (like you said). I can assign the right value, but since it is a relationship, it's not taking the change.
awcodes
awcodes2w ago
But the livewire component is available in any of the fields as an injection/resolution in the call back Right, you just have to get the call backs right.
bionary
bionaryOP2w ago
Since I am working outside the form though might have to do with why it's not accepting the programmatically set value.
awcodes
awcodes2w ago
If the select says the options are 1,2,3 but your form fill says default to 4 it’s just not going to work.
bionary
bionaryOP2w ago
yes, I get that That's why I examined the html directly to find an ID that I knew would be there. Then I hardcoded (tried setting that). Just to see if it would work, before things got dynamic
awcodes
awcodes2w ago
Also, may not be relevant, but default only works in the create context. In the edit context it’s assumed that the default has already been set during creation.
bionary
bionaryOP2w ago
that makes sense. Just to clarity: I am trying to programmatically set the form from outside the resource. It's a modal from the button in the header on the create page.
awcodes
awcodes2w ago
Fair but there’s still a lifecycle
bionary
bionaryOP2w ago
Just for ha-ha's I removed the relationship and added the simplest select options like this:
->options([
1 => 'one',
2 => 'two',
]),
->options([
1 => 'one',
2 => 'two',
]),
awcodes
awcodes2w ago
And relationships in selects don’t get loaded into the select until it is interacted with, so defaulting it isn’t necessary straight forward
bionary
bionaryOP2w ago
Then tried to set it:
$this->form->fill([
'parent_id' => 1 ,
]);
$this->form->fill([
'parent_id' => 1 ,
]);
Still doesn't work I think this is a dead end if that simple example doesn't work
awcodes
awcodes2w ago
To be fair, I don’t understand setting a form outside a resource. That doesn’t make sense to me.
bionary
bionaryOP2w ago
I am using a button to grab an existing resource from an external API and populate the form.
awcodes
awcodes2w ago
Ok, so what is the relation between the api and the select relationship?
bionary
bionaryOP2w ago
So while inside the create page, instead of filling out a form, I want to be able to use existing data from an external source The API sends an array and some of those values are used for various properties for that model. No relationship in a Laravel sense because it's external I can go about this another way and simply create the record on the fly from the index page...without having to fill in the form. it will 100% work, but I was just trying to fill in the form so I could see all the data coming over...like a preview of sorts before creating the model
awcodes
awcodes2w ago
Ok, im lost now. Sorry. What does the external api have to do with a select that is a relationship on the local database?
bionary
bionaryOP2w ago
Yeah, this is a tough one...sorry Adam !!! The API is from one of my wordpress websites. and my Filament app manages this website and several others.
awcodes
awcodes2w ago
No worries, just trying to understand the issue and it’s just not adding up to me logically.
bionary
bionaryOP2w ago
The select is for getting the heirarchy of the wordpress categories. (parent / child)
awcodes
awcodes2w ago
So, you’re trying to use filament to manage a wp install?
bionary
bionaryOP2w ago
Yes, been doing that for years!
awcodes
awcodes2w ago
Oh god. That’s a whole diffent beast.
bionary
bionaryOP2w ago
I built a personal system that posts to all kinds of websites 1000s of articles without ever touching wordpress! images, formatting everthing 🙂 it's such a mess and wordpress is the absolute worst...but it all works I tell ya
awcodes
awcodes2w ago
I admire what you are doing, but that’s not an easy thing to do.
bionary
bionaryOP2w ago
I did it years ago. it runs one of my businesses I can create categories but was going to implement a way to import/link exisiting categories. But like I said, I can do this no problem from the index. It will all happen once the dropdown is selected and I won't get to see the form filled, no big deal. Well I just talked myself into refactoring it that way! Thanks Adam
Dennis Koch
Dennis Koch2w ago
Yay, I'm not alone. Not posting to WordPress, but building some kind of "MainWP" in Filament
bionary
bionaryOP2w ago
@Dennis Koch oh sweet. Working with Wordpress is a nightmare. Doing the simplest things (for example: adding images) will make anyone want to jump off a building. I had to build a custom Wordpress plugin just to handle areas where the awful, antiquated and often incomplete WordPress API documentation falls short.
Dennis Koch
Dennis Koch2w ago
Building the API plugin was the most fun I ever had with WordPress 😅 Even though it's a lot of pain sometimes. I want to update my plugins via API: "Oh, nice there's a function. Seems easy" ... "Oh that doesn't work" ... "Oh, it only works when I require a different file, but it doesn't tell my and doesn't throw an error"
bionary
bionaryOP2w ago
@Dennis Koch sounds about right

Did you find this page helpful?