Auto Save on live()

Hi all I want my textfields to automatically save as I type. I thought just adding live() would be enough but I found I needed the following:
TextInput::make('telegram_username')
->live()
->afterStateUpdated(function (?TelegramUser $record, Field $component, $state) {
$record->{$component->getName()} = $state;
$record->save();
}),
TextInput::make('telegram_username')
->live()
->afterStateUpdated(function (?TelegramUser $record, Field $component, $state) {
$record->{$component->getName()} = $state;
$record->save();
}),
Is there a better way to do this with less code? I have a page with a lot of fields all needing live saving. Thanks all,
6 Replies
awcodes
awcodes12mo ago
This is going to kill your server and have uX issues. At the least you should use ->live(onBlur: true). That way it would save when blurring the field. To answer your question though, you can globally configure fields. https://filamentphp.com/docs/3.x/forms/fields/getting-started#global-settings I would highly recommend rethinking this approach though if you can.
David | Fortune Validator
thank you for coming back to me. I am happy to use the on blur or a debounce. I'm testing writting my works MIS system into filament. It only has a 5 users so its nothing crazy but it has a lot of functionality. My main sticking point is I dont trust them all to hit a SAVE button once finished ( older generation and past expereince ). As such an automatic saving has been working well on standard laravel/livewire with a debounce. I want to replicate this. thank you regarding the global settings. Is it possible to do this on a per resource / page basis do you think?
awcodes
awcodes12mo ago
No, it has to be called in a service provider and it’s global meaning it affects all of the fields it’s used on. Another approach would be to create a reusable function that you could pass into the modifier. ->afterStateUpdated($this->storeRecord(…) Pseudo code.
David | Fortune Validator
that would indeed be a little clearner. Thank you. On a side note, is it possible to move the Save and Cancel button above the form? Sorry if its in the docs. My brain is frazzled reading through it all and tinkering
awcodes
awcodes12mo ago
Directly, no. But in the gethHeaderActions method on your CreateRecord class you can use an
Action::make(‘custom_create’)
->action(‘create’)
Action::make(‘custom_create’)
->action(‘create’)
Since the action is already registered from the footer you can call it. But you can’t use it directly because the header actions aren’t actually inside the form. And you can do the same thing for edit the action there would be ‘save’
David | Fortune Validator
thank you 🙂
Want results from more Discord servers?
Add your server