Adding an action to a form in a livewire component not working

I have a livewire component with a form on it. I want to add a delete button, which I feel like should be pretty straight-forward. I see in the docs that I can add an anonymous action, but it's not clear on where I put it? Maybe this is obvious to others, but I've tried a few things: adding it to the mount method, using registerActions method on my form. I've tried following the instructions on adding it to a livewire component, including adding {{ $getAction('myaction') }} to the blade file, but that gives an error of Undefined variable $getAction. How can I add an action button to a form (not a field) on a livewire component? I just want a simple delete button with a confirmation modal. This is my form if it's helpful:
public function editTaskForm(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Textarea::make('description')->autosize(),
Select::make('assigned_to')
->options(fn (): array => $this->task->team->members->pluck('fullName', 'id')->toArray())
->searchable()
])
->model($this->task)
->statePath('taskData');
}
public function editTaskForm(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Textarea::make('description')->autosize(),
Select::make('assigned_to')
->options(fn (): array => $this->task->team->members->pluck('fullName', 'id')->toArray())
->searchable()
])
->model($this->task)
->statePath('taskData');
}
the action I'd like to add:
Action::make('delete')
->action(fn (MonthEndTask $record) => $record->delete())
->requiresConfirmation()
Action::make('delete')
->action(fn (MonthEndTask $record) => $record->delete())
->requiresConfirmation()
Solution:
```php public function deleteAction(): Action { return Action::make('delete') ->requiresConfirmation()...
Jump to solution
17 Replies
toeknee
toeknee13mo ago
$form->schema()->actions([ SaveAction::make(), DeleteAction::make() ]) or similar if memory serves, but I haven't done it on V3 yet
awcodes
awcodes13mo ago
if you need it 'in' the form then you need an anonymous action as i linked. If you want it at the end of the form, next to save, then toeknee's method is correct.
Jon Mason
Jon MasonOP13mo ago
Hmm…let me try that. I could’ve sworn I tried it and got an error, but maybe I’m crazy. ok, I'm getting the error Method Filament\Forms\Form::actions does not exist.
public function editTaskForm(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Textarea::make('description')->autosize(),
Select::make('assigned_to')
->options(fn (): array => $this->task->team->members->pluck('fullName', 'id')->toArray())
->searchable()
])->actions([
Action::make('delete')
->requiresConfirmation()
])
->model($this->task)
->statePath('taskData');
}
public function editTaskForm(Form $form): Form
{
return $form->schema([
TextInput::make('title')->required(),
Textarea::make('description')->autosize(),
Select::make('assigned_to')
->options(fn (): array => $this->task->team->members->pluck('fullName', 'id')->toArray())
->searchable()
])->actions([
Action::make('delete')
->requiresConfirmation()
])
->model($this->task)
->statePath('taskData');
}
I've got InteractsWithActions trait and the HasActions contract on my class
awcodes
awcodes13mo ago
are you doing this as a cutom livewire component or on a panels / resource Page? if it's just a custom livewire component then you wouldn't add the actions to the form, you would just render them in your view.
Jon Mason
Jon MasonOP13mo ago
ok yeah it's a custom livewire component. So I would just do it how I woudl in a non-filament app? Basically add the button and wire:click to the delete method? what if I want to show a confirmation modal, can I open that using $dispatch and pass an ID to it?
Solution
awcodes
awcodes13mo ago
public function deleteAction(): Action
{
return Action::make('delete')
->requiresConfirmation()
->action(fn($arguments) => do something with arguments);
}
public function deleteAction(): Action
{
return Action::make('delete')
->requiresConfirmation()
->action(fn($arguments) => do something with arguments);
}
{{ ($this->deleteAction)(['id' => $record->id]) }}

<x-filament-actions::modals />
{{ ($this->deleteAction)(['id' => $record->id]) }}

<x-filament-actions::modals />
awcodes
awcodes13mo ago
for example. don't need to do the manual wiring up on click etc.
Jon Mason
Jon MasonOP13mo ago
Ok, that's super helpful and I've almost got it working. The ->actions() call isn't doing anything though. Looking at the link you provided, seems to indicate I could do something like this:
public function deleteAction(): Action
{
return Action::make('delete')
->action(fn (MonthEndTask $task) => Log::debug($task))
->requiresConfirmation();
}
public function deleteAction(): Action
{
return Action::make('delete')
->action(fn (MonthEndTask $task) => Log::debug($task))
->requiresConfirmation();
}
However, nothing is logged. I do see in the network tab that it's sending the appropriate data. Here is my mount method:
public MonthEndTask $task;

public function mount(int $id = null): void
{
$this->taskId = $id;
$this->task = MonthEndTask::find($id) ?? new MonthEndTask();
$this->fillForms();
}
public MonthEndTask $task;

public function mount(int $id = null): void
{
$this->taskId = $id;
$this->task = MonthEndTask::find($id) ?? new MonthEndTask();
$this->fillForms();
}
I'm so close, I just must be doing something slightly wrong. I've tried modifying like so to see what happens:
return Action::make('delete')
->mountUsing(fn (MonthEndTask $task) => dd($task))
->action(fn (MonthEndTask $task) => Log::debug($task))
->requiresConfirmation()
->model($this->task);
return Action::make('delete')
->mountUsing(fn (MonthEndTask $task) => dd($task))
->action(fn (MonthEndTask $task) => Log::debug($task))
->requiresConfirmation()
->model($this->task);
->model() doesn't seem to do anything, and mountUsing() produces an empty model, so it's not evidently getting the task.
No description
Jon Mason
Jon MasonOP13mo ago
Figured it out:
return Action::make('delete')
->mountUsing(fn (array $arguments) => Log::debug$arguments))
->action(fn (array $arguments) => Log::debug($arguments))
->requiresConfirmation();
return Action::make('delete')
->mountUsing(fn (array $arguments) => Log::debug$arguments))
->action(fn (array $arguments) => Log::debug($arguments))
->requiresConfirmation();
Thanks for your help @awcodes and @toeknee !
Jon Mason
Jon MasonOP13mo ago
welp...I spoke too soon. The mountUsing will log a value, but the ->action() isn't doing anything. ergh.
Jon Mason
Jon MasonOP13mo ago
i think this a bug. I'm seeing others with an issue related to table actions. https://github.com/filamentphp/filament/issues/8924
GitHub
Table row actions not working on custom livewire page · Issue #8924...
Package filament/filament Package Version v3.0.68 Laravel Version v10.26.2 Livewire Version v3.0.5 PHP Version 8.1.19 Problem description I wanted to add a row action to a table element on a custom...
toeknee
toeknee13mo ago
fn(array $arguements, $data) => dd($data) do you get the data?
Jon Mason
Jon MasonOP13mo ago
no data, just a pause and then the page refreshes. it's weird because if I dd() in the mountUsing method, I get a value, it's just the action method that seems to be broken.
Nox
Nox13mo ago
GitHub
arguments passed to action modal content missing · Issue #8763 · fi...
Package filament/filament Package Version 3.0.62 Laravel Version 10.25.1 Livewire Version 3.0.5 PHP Version 8.1.23 Problem description when passing action arguments to modal content view it become ...
Nox
Nox13mo ago
your issue is probably this one, i hope it get resolve sone day....
Want results from more Discord servers?
Add your server