Form ->live() Not Working

HI Again, I have a field that the user needs to enter an increase value, I want this value to save on blur. I believe after reading the docs ->live() will do the job. I have the below code and it doesn't save to the Data Base on Blur but the afterStateUpdated() function does work? Am I missing something obvious here??
Forms\Components\TextInput::make('increase_percent')->label('% Increase')
->numeric()
->inputMode('decimal')
->live(onBlur: true)
->afterStateUpdated(function (?string $state, ?string $old) {
Notification::make()
->title('Price Increase Adjusted From ' . $old . ' to' . $state)
->success()
->send();
})
]),
Forms\Components\TextInput::make('increase_percent')->label('% Increase')
->numeric()
->inputMode('decimal')
->live(onBlur: true)
->afterStateUpdated(function (?string $state, ?string $old) {
Notification::make()
->title('Price Increase Adjusted From ' . $old . ' to' . $state)
->success()
->send();
})
]),
14 Replies
MazeEzam
MazeEzamOP10mo ago
Thank you but I am using the full filament package, do I still need to add a form to a component?
LeandroFerreira
LeandroFerreira10mo ago
Try without ->numeric()
MazeEzam
MazeEzamOP10mo ago
I removed
->numeric
->numeric
and also
->inputMode()
->inputMode()
but it still doesn't persist to the database on blur focus. The
afterStateUpdated()
afterStateUpdated()
still works as it should though.
LeandroFerreira
LeandroFerreira10mo ago
Is it sending the notification after state updated?
MazeEzam
MazeEzamOP10mo ago
Yes it is, just not updating the DB.
LeandroFerreira
LeandroFerreira10mo ago
Actually, live is working. If you want to save the record afterStateUpdated, you should do $livewire->save() when you are editing the record, and $livewire->create() when you are creating the record
MazeEzam
MazeEzamOP10mo ago
Ok, so what is the point of the
->live()
->live()
function if it doesn't actually do anything? I would have thought this would take care of the Livewire Create and Save etc. If I remove
->live()
->live()
afterStateUpdated still fires, so I'm not sure of the use case here? The Documentation says
->live()
->live()
should trigger the form to re render, is this not the case?
MazeEzam
MazeEzamOP10mo ago
I did, specifically "Reactive fields on blur" My understanding is that adding
->live()
->live()
to a form component would update the DB when the value is changed and the user navigates away from the field? I have to missing something very obvious here......
LeandroFerreira
LeandroFerreira10mo ago
No, live won't save the field on the DB
MazeEzam
MazeEzamOP10mo ago
Ok thanks, I have misunderstood this completely then. Using your suggestion above
$livewire->save()
$livewire->save()
I tried calling this in the
afterStateUpdated()
afterStateUpdated()
but threw and error "undefined variable $livewire" guessing I need to import a class for livewire?
MazeEzam
MazeEzamOP10mo ago
Thanks mate, this is now working as I need. This is the code I went with.
->afterStateUpdated(function (Livewire $livewire,?string $state, ?string $old) {

if ($old == null){
$old = '0.00';
return $old;
}
Notification::make()
->title('Price Increase Adjusted From ' . $old . '%' . ' to' . ' ' . $state . '%')
->success()
->send();

$livewire->save();
})
->afterStateUpdated(function (Livewire $livewire,?string $state, ?string $old) {

if ($old == null){
$old = '0.00';
return $old;
}
Notification::make()
->title('Price Increase Adjusted From ' . $old . '%' . ' to' . ' ' . $state . '%')
->success()
->send();

$livewire->save();
})
Want results from more Discord servers?
Add your server