F
Filament16mo ago
Abi

action method not working on a standalone Action

I have the following code and the action method doesn't get executed.
Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->modalHeading('Add Players to Cart')
->modalWidth('2xl')
->modalSubmitActionLabel('Add Players to Cart')
->modalFooterActionsAlignment('end')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->required()
->multiple()->options($this->getFamilyMembers()),
])->action(fn($data) => function () {
ray($this->form->getState());
});
Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->modalHeading('Add Players to Cart')
->modalWidth('2xl')
->modalSubmitActionLabel('Add Players to Cart')
->modalFooterActionsAlignment('end')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->required()
->multiple()->options($this->getFamilyMembers()),
])->action(fn($data) => function () {
ray($this->form->getState());
});
and the blade content is the following
{{ ($this->addToCartAction)(['productId'=>$item->id])}}
{{ ($this->addToCartAction)(['productId'=>$item->id])}}
10 Replies
Vp
Vp16mo ago
Ref to this docs https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component#passing-action-arguments, it should be like {{ ($this->addToCart)(['productId'=>$item->id]) }} Notice Action is removed
Abi
AbiOP16mo ago
I tried that, still have the same issue The modal shows up correctly, but the action method is never getting executed. The action is actually inside a @foreach loop, I thought may be that could be the issue, so I removed it from the loop and it still doesn't work
Vp
Vp16mo ago
Did you try dd($data) or ray($data) instead of $this
Abi
AbiOP16mo ago
yes, tried both nothing, it just closes the modal, it doesn't refresh the page. I checked the network calls on the dev tools. No errors all the validations seem to work correctly in the modal form, but the submit button just closes the modal and doesn't execute the action method
Vp
Vp16mo ago
Did you put your action inside public function like the docs, like this
public function deleteAction(): Action
{
return Action::make('delete')
...
}
public function deleteAction(): Action
{
return Action::make('delete')
...
}
Abi
AbiOP16mo ago
Yes every other functionality of that action method works except for the action method of the Action class
public function addToCartAction(): Action
{
return Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->modalHeading('Add Players to Cart')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->required()
->multiple()->options($this->getFamilyMembers()),
])->action(fn($data) => function () {
dd('Here');
ray($this->form->getState());
});
}
public function addToCartAction(): Action
{
return Action::make('addToCart')
->label('ADD PLAYERS TO CART')
->modalHeading('Add Players to Cart')
->form([
Select::make('existing_kids')
->label('Select existing kids information previously added')
->required()
->multiple()->options($this->getFamilyMembers()),
])->action(fn($data) => function () {
dd('Here');
ray($this->form->getState());
});
}
the modal opens correctly, the validation for the Select component works correctly, just not the ->action method
Vp
Vp16mo ago
In the docs, they put ->action(function (array $arguments), can you try with $arguments.. This is stand alone Action and I don't have time to reproduce rn.. other than this I don't have any idea tho
Abi
AbiOP16mo ago
they have both $arguments and $data, tried both ok, found the issue and wasted so much time it was action(fn($data) => function () { I don't know why I created a fn and pointed to an function. That was just so stupid to not notice it at all.. changed it to action(function ($data) { and it worked
Vp
Vp16mo ago
haha.. learn from mistake 😆
Abi
AbiOP16mo ago
Thanks for the help and your time
Want results from more Discord servers?
Add your server