FilamentF
Filament2y ago
5 replies
Grégoire

Using refreshFormData() in static form methods

Hello everyone,

I am a little confused by the "Refreshing form data" part of the documentation. It states that if "you're using actions on an Edit or View resource page, you can refresh data within the main form" which is exactly what I want.

The example provided is:

Action::make('approve')
    ->action(function (Post $record) {
        $record->approve();
 
        $this->refreshFormData([
            'status',
        ]);
    })


The thing is that a resource page's form method is static, and therefore it is not possible to call $this->refreshFormData() from within the action method.

The obvious error (if someone searches by keyword):
Using $this when not in object context


What am I missing here?

class DocumentResource extends Resource
{
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('AI generated content')
                    ->headerActions([
                        Action::make('Auto-generate fields')
                            ->icon('heroicon-m-sparkles')
                            ->action(function (Document $record) {
                                UpdateDocumentMetadata::run($record);
                                $form->refreshFormData();
                            })
                    ])
                    ->schema([
                        // ...


I suppose there is a way to inject the property into the function?
Solution
In this context, iirc, you can inject $livewire into the ->action() callback then call it off of the $livewire object.
Was this page helpful?