F
Filamentβ€’7mo ago
Adam Holmes

Prompt input via modal on save

Hi, I have a status field on a form which is a ToggleButton populated by an enum. I would like to add some functionality so that when I save the form, if this field has changed to a specific option then a modal should appear and the user should enter a reason (which is just a text area). I would like the reason to save into a different database table as the form I'm on because I'd like to save individual rows for each change so that I can report on all the changes later (rather than overwriting the reason each time). I'm at a real loss of how to make this happen as I'm fairly new to filament. I managed to make it overwrite the data on the same model but hiding / showing a field, but that's not what I want - I want a modal to popup on save. I also tried to add a relationship to a Group but that is still a HasOne where I would like a HasMany. Any ideas or pointers in the right direction would be much appreciated. Thanks
Solution:
You could register an action and trigger it in afterStateUpdated ```php ->registerActions([ Forms\Components\Actions\Action::make('reason')...
Jump to solution
4 Replies
Adam Holmes
Adam HolmesOPβ€’7mo ago
I'm wondering whether the easiest way would be to avoid using a modal completely. Just conditionally show a field and then save it to the relationship in the beforeSave method?
Solution
LeandroFerreira
LeandroFerreiraβ€’7mo ago
You could register an action and trigger it in afterStateUpdated
->registerActions([
Forms\Components\Actions\Action::make('reason')
->form([
Forms\Components\Textarea::make('message'),
])
->action(function (array $data) {
//save into a different database table..
}),
])
->afterStateUpdated(function($state, Page $livewire, Forms\Components\Component $component){

if($state === 'xxx) {
$livewire->mountFormComponentAction($component->getStatePath(), 'reason');
}
})
->registerActions([
Forms\Components\Actions\Action::make('reason')
->form([
Forms\Components\Textarea::make('message'),
])
->action(function (array $data) {
//save into a different database table..
}),
])
->afterStateUpdated(function($state, Page $livewire, Forms\Components\Component $component){

if($state === 'xxx) {
$livewire->mountFormComponentAction($component->getStatePath(), 'reason');
}
})
LeandroFerreira
LeandroFerreiraβ€’7mo ago
you need to add live() in the Toggle
Adam Holmes
Adam HolmesOPβ€’7mo ago
Amazing! This is exactly what I needed. Half the battle with learning Filament is knowing the best way to approach something - or at least what to search for in the docs. Thanks πŸ™‚
Want results from more Discord servers?
Add your server