How to make the Save button not include custom form field in the saving process?

I have a separate "save" feature included in my custom code that is independent on the Save of the form. It works great. I do what I want via wire:click, stuff is saved directly in the DB as I need. When I leave the form and go to other parts of Admin panel, things are persisted in the DB without the need of pressing the Save button for this record. Now, the problem arises when I am editing the record and press the Save button (the one for the whole form). It messes up the saving of the custom form field. Not sure what it does, probably grabs some wrong values because of bad/stale wire:key or something... the point is that: I do NOT want the Save button to save things in the db for this custom field. I want to exclude this field from being saved with the Save button. I set ->dehydrate(false) on my custom form field and it seems to be working as long as I press the Save button. Then it saves weird things in that DB column. If I remove ->dehydrate(false) or just leave ->dehydrate() I will get the Column not found SQL error. How to make Save ignore this custom field when updating/saving post?
Solution:
I told you above how to remove them.
Jump to solution
21 Replies
Wirkhof
Wirkhof4mo ago
I have tried to put ->disabled() on the field as well, but it's still saving when pressing Save. How to disregard this custom field from being saved by pressing the Save button? Can it be done in the EditCar.php file? One of those 3 files generated by the resource generator?
toeknee
toeknee4mo ago
How are you handling save?
Wirkhof
Wirkhof4mo ago
Livewire calls to method
toeknee
toeknee4mo ago
Show the call
Wirkhof
Wirkhof4mo ago
in EditCar.php
toeknee
toeknee4mo ago
sorry method
Wirkhof
Wirkhof4mo ago
We talked yesterday. That part is working great. Basically everything works
toeknee
toeknee4mo ago
so if you are using livewire custom component, if you use: $this->form->getState() it will ensure it's passed the method for all the form
Wirkhof
Wirkhof4mo ago
except that the Save button at the end of the whole form is messing stuff. ok, so can I add a method that will overwrite the Save button process in EditCar.php?
toeknee
toeknee4mo ago
$data = $this->form->getState() should be the data to use if it's cusotm livewire. but without seeing the whole code I can't advise.
Wirkhof
Wirkhof4mo ago
getSaveFormAction(): I have alread ythis function protected function getSaveFormAction(): \Filament\Actions\Action { } in EditCar.php now I need to say it to strip my custom field from the $data array or how it's called
toeknee
toeknee4mo ago
Please provide the whole EditCar.php
Wirkhof
Wirkhof4mo ago
protected function getSaveFormAction(): \Filament\Actions\Action
{
return parent::getSaveFormAction()
->disabled(fn () => ($this->record->current_state == 'banned'));
}
protected function getSaveFormAction(): \Filament\Actions\Action
{
return parent::getSaveFormAction()
->disabled(fn () => ($this->record->current_state == 'banned'));
}
I am pretty sure I have to work with this function I know there is $data somethign And I have to get rid of $data['mycustomfiled'] before it reaches saving. Could it be via this method? mutateFormDataBeforeSave()
toeknee
toeknee4mo ago
Without seeing your whole code I can't advise sorry.
Wirkhof
Wirkhof4mo ago
protected function mutateFormDataBeforeSave(array $data): array
{
$data['mycustomfield'] = ?????;

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['mycustomfield'] = ?????;

return $data;
}
How to not include (or delete) mycustomfield from $data? I almost have it I just need to remove / not include it in data in this mutateFormBeforeSave function
toeknee
toeknee4mo ago
If you want to use mutate you can just unset($data['my_field']); return $data; But dehydrated should be working and shouldn't need mutateForm before saving.
Wirkhof
Wirkhof4mo ago
Ok i will try unset it OK, so this:
protected function mutateFormDataBeforeSave(array $data): array
{
$data['user_id'] = 99;

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['user_id'] = 99;

return $data;
}
is being triggered on each Save press and the user_id changes accordingly as I change it in the code. So, this works. Now, I need to remove my custom field from return $data. How to do that? OK, I did it and this works:
protected function mutateFormDataBeforeSave(array $data): array
{

unset($data['mycustomfield']);

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{

unset($data['mycustomfield']);

return $data;
}
Solution
toeknee
toeknee4mo ago
I told you above how to remove them.
toeknee
toeknee4mo ago
But as I said, that is wrong and you shouldn't need to be unsetting them if you are disabling them they should not be included in save.
Wirkhof
Wirkhof4mo ago
It's funny though, maybe because it was in the wrong name of the field 😉 from the beginning Yeah, now if I comment out the mutateFormDataBeforeSave method it works as well. All I need is to have ->dehydrated(false) and a correct column name in the table. The column name was wrong. Perhaps that was the problem. Thanks a lot for help toeknee.
Wirkhof
Wirkhof4mo ago
So, for future visitors, check the name of your column (if it is the same as in your db) and use dehydrate(false) it should exclude that field from saving via the default Save button. Only then try to alter things in $data via mutateFormDataBeforeSave() etc.