refreshFormData in afterSave() function
Hello, simply question:
i try to update a form field after saving data, reading a value from database.
in edit page add this function
protected function afterSave(): void
{
$this->refreshFormData([
'calorie_calc',
]);
}
but not work... any idea?
Regards
16 Replies
Try similar to thishttps://stackoverflow.com/questions/74527583/filamentphp-ho-to-refresh-re-calculate-the-count-value-on-header
Stack Overflow
Filamentphp, Ho to refresh/re-calculate the count value on header
I made CommentsRelationManager for PostResource.
How I can refresh/recalculate the count value on header, whatever the comments is added or deleted.
Image note : I add a new comment, but the count...
thanks mate
You could calcualte it in a view field or similar though on the fly based on changes in the form
@toeknee mmm debugging the code, the problem is slightly different
the function refresh the field...but not take the last value save in db but the previous...
(consider that the field in db is virtual... if i refresh page the result is correct)
protected function afterSave(): void
{
$this->refreshFormData([
'calorie_calc',
]);
}
How about this ?
not work..and not understand why....
work with $this->form->fill([
Where does the calorie_calc value come from? why are you trying to update it after save?
Solution
Ok.. just use $this->form->fill($this->record->refresh()->toArray())
now work 🙂 ..but why not work with refreshFormData?
Is this a custom page?
i try this....and not work
$this->refreshFormData([
'calorie_calc' => 13
]);
no simple EditRecord
So viewRecord and EditRecord has the refreshFormData which does
So if you pass in the correct attribute it should trump it...
try
Method Filament\Forms\Form::refreshFormData does not exist.
Sorry updated
$this->form->fill($this->record->refresh()->toArray()); this work correctly
I just wanted to understand if refreshFormData couldn't be used
So it should be used can can be, it's defined It could be that it's too late in the cycle. But either way, if the fill works and that's all refresh does... then win win!