F
Filament•4mo ago
Albert Lens

getFormActions / getCreateFormAction - Need to customize the save button to add more functionality

I have a resource in which I need to change the SAVE button by a new one which saves and also adds some function that I have prepared. How can I achieve this, please? I have tried adding this function to the CreateResource.php:
protected function getFormActions(): array
{
return [
$this->getCreateFormAction()->label('Crear traspaso contable')->color('primary'),
];
}
protected function getFormActions(): array
{
return [
$this->getCreateFormAction()->label('Crear traspaso contable')->color('primary'),
];
}
But I do not know how to: - Add my special function to that button (I want it to add the record to the table and do other things as well) Any ideas, please? I have also tried using an observer, which works fine, but then the problem is knowing how to: - change or delete the notification typical message of "created", because my other function also gives a message and one is shown covering the other. Tks.
Solution:
have you checked the Customizing the creation process in the docs? https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-the-creation-process also check the Lifecycle hooks in that same page. If that doesn't cover you needs, then you need to create a Laravel Action, not a Filament action, there you will handle everything about your saving process and then maybe remove the default save button and replace it with your own, so that it invokes your action. Does this help?...
Jump to solution
2 Replies
Solution
🤖transistor🤖
have you checked the Customizing the creation process in the docs? https://filamentphp.com/docs/3.x/panels/resources/creating-records#customizing-the-creation-process also check the Lifecycle hooks in that same page. If that doesn't cover you needs, then you need to create a Laravel Action, not a Filament action, there you will handle everything about your saving process and then maybe remove the default save button and replace it with your own, so that it invokes your action. Does this help?
Albert Lens
Albert Lens•4mo ago
Hello and thank you. Hooks is the solution. I disabled the normal CREATE button and created an Action in the headerActions section and everything is fine. User only sees my new button and if you click it does all the functioning I have decided. Perfect.
->headerActions([
Action::make('Generar nuevo traspaso contable')
->color('amber')
->action(function () {
$cuenta=Cobro::where('ctc',null)->count();
$cuenta2=Pago::where('ctc',null)->count();
$total = $cuenta + $cuenta2;
if ($total==0) {

... etc ... etc ...

}),
],position: HeaderActionsPosition::Bottom)
->headerActions([
Action::make('Generar nuevo traspaso contable')
->color('amber')
->action(function () {
$cuenta=Cobro::where('ctc',null)->count();
$cuenta2=Pago::where('ctc',null)->count();
$total = $cuenta + $cuenta2;
if ($total==0) {

... etc ... etc ...

}),
],position: HeaderActionsPosition::Bottom)
Thank you for your time.