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?21 Replies
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?How are you handling save?
Livewire calls
to method
Show the call
in EditCar.php
sorry method
We talked yesterday.
That part is working great.
Basically everything works
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
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?
$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.
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
Please provide the whole EditCar.php
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()
Without seeing your whole code I can't advise sorry.
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
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.
Ok i will try unset it
OK, so this:
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:
Solution
I told you above how to remove them.
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.
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.
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.