FilamentF
Filamentβ€’2y ago
1benjam1

Refresh my form after custom "Cancel" action

Hi guys. I created an action that allow to cancel a mission (switch status + fill cancellation related fields). Those fields are visible on my MissionResource.php only if the status is CANCELLED.

Forms\Components\Section::make(__('Cancellation'))
    ->schema([
        Forms\Components\DateTimePicker::make('cancelled_at')
            ->label(__('Cancelled at'))
            ->displayFormat('d.m.Y H:i')
            ->native(false),
        Forms\Components\Select::make('cancellation_type')
            ->label(__('Cancellation type'))
            ->options(CancellationTypeEnum::class)
            ->required(),
        Forms\Components\Textarea::make('cancellation_reason')
            ->label(__('Cancellation reason'))
            ->rows(3),
    ])
    ->visible(fn (?Mission $record): bool => $record && $record->mission_status === MissionStatusEnum::CANCELLED),


The problem is, when the action is performed, the fields appears but they are not fill with the data I just typed, but those that where loaded at the loading of the page. I tried to use protected $listeners = ['refreshEditMissions' => '$refresh']; on the EditMissions.php page and dispatch the event in the action, I tried to use the refreshFormData method, but nothing is working. Please help πŸ™

Here is my action code : https://pastebin.com/LNDJSQt6
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Solution
I finally find a solution here : https://v2.filamentphp.com/tricks/update-main-form-after-relationship-manager-crud

On EditResource.php:
use Livewire\Attributes\On;
 
#[On('refreshForm')]
public function refreshForm(): void
{
    parent::refreshFormData(array_keys($this->record->toArray()));
}

Then you can call it with (p.e. inside an action):
$action->getLivewire()->dispatch('refreshForm');
Filament
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
Was this page helpful?