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
Guess it depends on the select and what the available options are.
My Select looks like this:
I thought I could assign the id like:
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.
let me look at the hmtl output in console
If the select is a relationship then that relationship might not exist so you have to handle that.
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: 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.
I tried hardcoding in a value that I can see it there in google console: like this: 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.
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.
yes, that is what I am thinking.
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.
yes
If that makes sense.
it does
So just make the select preload with that key and it should be fine.
I thought I could programmatically select the value using
Set $set
but that is not working in this contextRight, 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.
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()
Preload can define a query on the relationship that includes the defaults.
The rest of the form fields (text input) fill in.
okay, let me look at the preload docs
Maybe you need to access the livewire instance in the action to get the relationship.
Hard to say without seeing the actual code.
I do appreciate the help Adam, thank you.
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.
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;
I was referring to DI, like in the select you can do something like fn($record) => doSomethingwith($record)
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.
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.
Since I am working outside the form though might have to do with why it's not accepting the programmatically set value.
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.
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
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.
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.
Fair but there’s still a lifecycle
Just for ha-ha's I removed the relationship and added the simplest select options like this:
And relationships in selects don’t get loaded into the select until it is interacted with, so defaulting it isn’t necessary straight forward
Then tried to set it:
Still doesn't work
I think this is a dead end if that simple example doesn't work
To be fair, I don’t understand setting a form outside a resource. That doesn’t make sense to me.
I am using a button to grab an existing resource from an external API and populate the form.
Ok, so what is the relation between the api and the select relationship?
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
Ok, im lost now. Sorry. What does the external api have to do with a select that is a relationship on the local database?
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.
No worries, just trying to understand the issue and it’s just not adding up to me logically.
The select is for getting the heirarchy of the wordpress categories. (parent / child)
So, you’re trying to use filament to manage a wp install?
Yes, been doing that for years!
Oh god. That’s a whole diffent beast.
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
I admire what you are doing, but that’s not an easy thing to do.
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
Yay, I'm not alone. Not posting to WordPress, but building some kind of "MainWP" in Filament
@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.
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"
@Dennis Koch sounds about right