Auto save for forms

Anyone here knows if there is a way to implement an autosave in a form? I'm writing articles and it took me a long time. Sometimes I close the tab unintentionally. I think it would be good to save my form automatically each 30 seconds, probably using polling.
Solution:
@pablopetr you can add the following method to the Pages/Edit{Resource}.php ```php public function updated(string $propertyName): void {...
Jump to solution
5 Replies
Povilas K
Povilas K2w ago
I remember we made it work for Wizard, so here's how to save the data. Now you need to figure out how to call this every 30 seconds or whatever: https://filamentexamples.com/tutorial/form-wizard-auto-save-draft-after-first-step
Filament Examples
Filament Form Wizard: Auto-Save Draft After First Step
Wizard forms solve the issue of having too many input fields (big forms). But what if the user leaves mid-filling? In that case, you can auto-save a draft and let them edit the record at any point.
Solution
rhysleesdev
rhysleesdev2w ago
@pablopetr you can add the following method to the Pages/Edit{Resource}.php
public function updated(string $propertyName): void
{
$this->save(
shouldRedirect: false,
shouldSendSavedNotification: false
);
}
public function updated(string $propertyName): void
{
$this->save(
shouldRedirect: false,
shouldSendSavedNotification: false
);
}
then add ->live() to the form elements and when they update it will call this method, then save. note the propertyName is passed to the update method, so if you have some other elements you dont want to trigger the save on you can filter them out.
toeknee
toeknee2w ago
You need to onBlur the live at the very least otherwise that's a shed ton of database updates as you will be updating on every key change
rhysleesdev
rhysleesdev2w ago
yup sorry, didnt mention it
pablopetr
pablopetrOP2w ago
Thank you very much @Rhys Lees, @PovilasKorop and @toeknee . I applied this solution. In my case I added ->debounce(10000) to the text input at my resource and the updated function in the Pages/Edit{Resource}.php: public function updated(): void { $this->save(); } This feature is to cases when I start thinking and let the keyboard alone. In this case the database updates will not be a problem. Also, to see the notifications is good to me.

Did you find this page helpful?