F
Filament2w ago
JC

$livewire->form->fill() doesn't work when using slideOver()

I've been following Kevin McKee's excellent "Rapid Laravel Apps with Filament" Laracast but run into an issue when trying to use a factory to fill a form (for testing purposes). When my resource ("Campaign") has a dedicated CreateCampaign route/page, I'm able to add an action to the form as follows:
Actions::make([
Action::make('star')
->label('Insert test data')
->icon('heroicon-m-pencil-square')
->visible(function (string $operation) {
return $operation === 'create' && app()->environment('local');
})
->action(function (Livewire $livewire) {
$data = Campaign::factory()->make()->toArray();
$livewire->form->fill($data);
}),
])
Actions::make([
Action::make('star')
->label('Insert test data')
->icon('heroicon-m-pencil-square')
->visible(function (string $operation) {
return $operation === 'create' && app()->environment('local');
})
->action(function (Livewire $livewire) {
$data = Campaign::factory()->make()->toArray();
$livewire->form->fill($data);
}),
])
and this works great. However, I'd prefer to use the slideOver() so I comment out 'create' => Pages\CreateCampaign::route('/create'), in the getPages() function of the campaign resource and add ->slideOver() to the getHeaderActions of the ListCampaigns file (no other changes made). I then get the following error:
Filament\Resources\Pages\ListRecords::form(): Argument #1 ($form) must be of type Filament\Forms\Form, Filament\Infolists\Infolist given, called in /Users/joe/Projects/test_system/vendor/filament/infolists/src/Concerns/InteractsWithInfolists.php on line 56
It looks like, when using a slideOver, $livewire is the ListCampaign component rather than the CreateCampaign page so the form is not accessible through $livewire->form. I'm sure there is an easy workaround but I'm pretty new to all this. Thanks 🙂
31 Replies
awcodes
awcodes2w ago
A slideOver would indicate a modal create action, but you’re telling the resource that the create action is a route. Not sure what you’re trying to do. It’s either a route or it isn’t. It can’t be both.
JC
JCOP2w ago
I started with it as a route to check that the "Insert test data" button was working (it was) but then removed the route and switched to the slideOver which is where the error started occurring.
The Actions... snippet above is part of the form definition of the CampaignResource so I thought that the $livewire reference would be the form but it seems to be the ViewCampaigns component (which is the one that has the modal-triggering button on it). I'm just looking for a way to fill a slideOver form 🙂 given that $livewire->form doesn't seem to work
awcodes
awcodes2w ago
Ok, but slideOver isn’t a route, but your resource is defining a route. It’s 2 different things. Also, the resource isn’t a livewire component. The resource is just a class that the view, edit, create and list livewire components can reference.
JC
JCOP2w ago
"but your resource is defining a route" - in what way? I thought removing the create route from getPages() was sufficient if I planned on using a modal?
awcodes
awcodes2w ago
What is the actual code of your header action and what classes are you including it on? Also, form()->fill() only works in mount of a livewire component. So for actual actions there is a ->fillForm() modifier that achieves the same thing. Just not sure of the use case of what you are actually trying to achieve.
JC
JCOP2w ago
This is my app/Filament/Admin/Resources/CampaignResource.php https://gist.github.com/joeczucha/4512e9c3726141d64ae66f9e723080f0 line 74+
awcodes
awcodes2w ago
Ok, you can’t fill the form after initialization. At the point you should be using $set to modify the form data. Fill form only applies to the form initialization which happens on mount.
JC
JCOP2w ago
ah I see. I had looked at $set but could only find examples of doing one field at a time rather than the whole form
awcodes
awcodes2w ago
Yeah, setting the entire form based on a field doesn’t make sense to me. I could be wrong though.
JC
JCOP2w ago
the use case that I have is trying to get the button on this form ("Insert test data") to populate the form with my factory test data
No description
JC
JCOP2w ago
and it works in this layout.
JC
JCOP2w ago
but not in this one which is what we use everywhere
No description
Matthew
Matthew2w ago
I'm pretty sure $this->form->fill() should be enough
JC
JCOP2w ago
thanks Matthew, I tried a few combinations of $this, $self, etc. Your example gives me this error:
Using $this when not in object context
Matthew
Matthew2w ago
Can I see the code?
Matthew
Matthew2w ago
You can't put $this in static functions 😅
JC
JCOP2w ago
line 84 is where I'm calling it
Matthew
Matthew2w ago
I see line 84 has $livewire, that doesn't work also, right?
JC
JCOP2w ago
so when I have a dedicated CreateCampaign route, the $livewire in line 84 allows me to do $livewire->form->fill() and it works. but when I use a slideOver the $livewire reference is the ViewCampaign component (i.e. the page with the button on). so I guess I need a way to access the form via the ViewCampaign page.
Matthew
Matthew2w ago
Very interesting...
awcodes
awcodes2w ago
A route doesn’t work in a modal.
JC
JCOP2w ago
so no way to fill the form, except for using $set?
awcodes
awcodes2w ago
It’s not that you can’t fill a form it’s that how you’re trying to do it doesn’t make sense to me because it’s too late in the life cycle of the request.
JC
JCOP2w ago
okay I think I understand. so what you're saying is that outputting a form in a modal vs it's own route significantly changes how it's rendered and in the case of the modal it's then too late to create an action to fill it?
awcodes
awcodes2w ago
Kinda. Hard to explain. Depends on several factors. It’s all about the lifecycle Either way you can’t depend on a route for create but also expect create to work in a modal at the same time. It just doesn’t work that way. And that not a filament issue.
JC
JCOP2w ago
Can I just ask which aspect of the code suggests that I'm trying to use a route AND use a modal? If I've removed the Create line from getPages() array does that not disable the route?
awcodes
awcodes2w ago
Maybe a caching issue.
JC
JCOP2w ago
I'm still no closer to getting this working, if anyone else has any thoughts 🙂
LeandroFerreira
->action(function (Pages\ListCampaigns $livewire) {
$data = Campaign::factory()->make()->toArray();
$livewire->mountedActionsData[0] = $data;
})
->action(function (Pages\ListCampaigns $livewire) {
$data = Campaign::factory()->make()->toArray();
$livewire->mountedActionsData[0] = $data;
})
JC
JCOP2w ago
amazing, thank you so much @Leandro Ferreira

Did you find this page helpful?