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
Solution:
Ok.. just use $this->form->fill($this->record->refresh()->toArray())
Jump to solution
16 Replies
toeknee
toeknee6d ago
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...
Zoltar
ZoltarOP6d ago
thanks mate
toeknee
toeknee6d ago
You could calcualte it in a view field or similar though on the fly based on changes in the form
Zoltar
ZoltarOP6d ago
@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', ]); }
toeknee
toeknee6d ago
protected function afterSave(): void
{
$this->refreshFormData([
'calorie_calc' => $this->record->refresh()->calorie_calc
]);
}
protected function afterSave(): void
{
$this->refreshFormData([
'calorie_calc' => $this->record->refresh()->calorie_calc
]);
}
How about this ?
Zoltar
ZoltarOP6d ago
not work..and not understand why.... work with $this->form->fill([
toeknee
toeknee6d ago
Where does the calorie_calc value come from? why are you trying to update it after save?
Solution
toeknee
toeknee6d ago
Ok.. just use $this->form->fill($this->record->refresh()->toArray())
Zoltar
ZoltarOP6d ago
now work 🙂 ..but why not work with refreshFormData?
toeknee
toeknee6d ago
Is this a custom page?
Zoltar
ZoltarOP6d ago
i try this....and not work $this->refreshFormData([ 'calorie_calc' => 13 ]); no simple EditRecord
toeknee
toeknee6d ago
So viewRecord and EditRecord has the refreshFormData which does
public function refreshFormData(array $attributes): void
{
$data = [
...$this->data,
...Arr::only($this->getRecord()->attributesToArray(), $attributes),
];

$this->form->fill($data);
}
public function refreshFormData(array $attributes): void
{
$data = [
...$this->data,
...Arr::only($this->getRecord()->attributesToArray(), $attributes),
];

$this->form->fill($data);
}
So if you pass in the correct attribute it should trump it... try
$this->refreshFormData($this->record->refresh()->toArray())
$this->refreshFormData($this->record->refresh()->toArray())
Zoltar
ZoltarOP6d ago
Method Filament\Forms\Form::refreshFormData does not exist.
toeknee
toeknee6d ago
Sorry updated
Zoltar
ZoltarOP6d ago
$this->form->fill($this->record->refresh()->toArray()); this work correctly I just wanted to understand if refreshFormData couldn't be used
toeknee
toeknee6d ago
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!

Did you find this page helpful?