F
Filament9mo ago
dyo

Problem with afterStateUpdated in ViewField

Why after I upgraded my Filament to v3, the afterStateUpdated in ViewField is not working anymore? I want to fill other inputs based on data in ViewField.
Solution:
nevermind, I'll just found out how to call livewire method from alpine..
Jump to solution
14 Replies
LeandroFerreira
LeandroFerreira9mo ago
could you share the code?
dyo
dyoOP9mo ago
Here..
ViewField::make('prospek_id')
->view('forms.components.cari-donatur')
->viewData([
'donatur' => Prospek::where('prioritas_id', auth()->user()->prioritas_id)
->select('id', 'nama', 'hp')->get()
])
->label('Cari Donatur (No HP/Nama)')
->afterStateUpdated(function (\Filament\Forms\Set $set, callable $get) {
$id_prospek = $get('prospek_id');
$prospek = Prospek::find($id_prospek);

if ($prospek) {
$set('nama', $prospek->nama);
$set('prioritas_id', $prospek->prioritas_id);
}
})
->required()->columnSpanFull(),
ViewField::make('prospek_id')
->view('forms.components.cari-donatur')
->viewData([
'donatur' => Prospek::where('prioritas_id', auth()->user()->prioritas_id)
->select('id', 'nama', 'hp')->get()
])
->label('Cari Donatur (No HP/Nama)')
->afterStateUpdated(function (\Filament\Forms\Set $set, callable $get) {
$id_prospek = $get('prospek_id');
$prospek = Prospek::find($id_prospek);

if ($prospek) {
$set('nama', $prospek->nama);
$set('prioritas_id', $prospek->prioritas_id);
}
})
->required()->columnSpanFull(),
In this ViewField, user is searching for prospek_id based on name or phone, using api endpoint to return the selected data.
LeandroFerreira
LeandroFerreira9mo ago
How are you updating this property and sending it to the server? Are you using something like wire:model.live="{{ $getStatePath() }}" ?
dyo
dyoOP9mo ago
Yes..
LeandroFerreira
LeandroFerreira9mo ago
It was supposed to work.. could you share?
dyo
dyoOP9mo ago
the view blade? It's quite long script https://codeshare.io/de6AXg I tried simplified the afterStateUpdated method with
->afterStateUpdated(function (\Filament\Forms\Set $set, callable $get) {
return $set('nama', 'test');
}
->afterStateUpdated(function (\Filament\Forms\Set $set, callable $get) {
return $set('nama', 'test');
}
Still not updated in nama property.
LeandroFerreira
LeandroFerreira9mo ago
try a simple input as <input type="text" wire:model.live="data.prospek_id"> and check if it works I think this should be data.prospek_id if you are using a resource
dyo
dyoOP9mo ago
the prospek_id input is updated with type text after user select the option, but the other input in afterStateUpdated is not..
LeandroFerreira
LeandroFerreira9mo ago
first of all, you should check if the reactivity is working with a simple input
dyo
dyoOP9mo ago
thanks for your answers, I'll try.. I just found out, that from filament v2, I updated the state from js, saddly it's not working anymore in v3. I updated the state in js function with
this.$wire.prospek_id = option.id
this.$wire.prospek_id = option.id
How can I trigger the state to be updated from js? you can check my js method in line 86
LeandroFerreira
LeandroFerreira9mo ago
$wire.prospek_id ?
dyo
dyoOP9mo ago
it shows $wire is not defined in console i tried using state, still not triggered to update I try another way to fill other input also from the js with
this.$wire.nama = option.nama;
this.$wire.nama = option.nama;
but I need some php script to run after i updated the prospek_id state, in afterStateUpdated.
Solution
dyo
dyo9mo ago
nevermind, I'll just found out how to call livewire method from alpine..
dyo
dyoOP9mo ago
still, I don't understand what is the cause problem of this thread

Did you find this page helpful?