F
Filament4w 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
could you share the code?
dyo
dyo4w 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
How are you updating this property and sending it to the server? Are you using something like wire:model.live="{{ $getStatePath() }}" ?
dyo
dyo4w ago
Yes..
LeandroFerreira
It was supposed to work.. could you share?
dyo
dyo4w 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
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
dyo4w ago
the prospek_id input is updated with type text after user select the option, but the other input in afterStateUpdated is not..
LeandroFerreira
first of all, you should check if the reactivity is working with a simple input
dyo
dyo4w 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
$wire.prospek_id ?
dyo
dyo4w 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
dyo4w ago
nevermind, I'll just found out how to call livewire method from alpine..
dyo
dyo4w ago
still, I don't understand what is the cause problem of this thread
Want results from more Discord servers?
Add your server
More Posts