#refreshFormData in afterSave() function

21 messages · Page 1 of 1 (latest)

prime dune
#

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

sick iglooBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

dire epoch
#

Try similar to thishttps://stackoverflow.com/questions/74527583/filamentphp-ho-to-refresh-re-calculate-the-count-value-on-header

prime dune
#

thanks mate

dire epoch
#

You could calcualte it in a view field or similar though on the fly based on changes in the form

prime dune
#

@dire epoch 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',
]);
}

dire epoch
#

 protected function afterSave(): void
    {
        $this->refreshFormData([
            'calorie_calc' => $this->record->refresh()->calorie_calc
        ]);
    }

How about this ?

prime dune
#

not work..and not understand why....

#

work with $this->form->fill([

dire epoch
#

Where does the calorie_calc value come from? why are you trying to update it after save?

#

Ok.. just use $this->form->fill($this->record->refresh()->toArray())

prime dune
#

now work 🙂 ..but why not work with refreshFormData?

dire epoch
#

Is this a custom page?

prime dune
#

i try this....and not work
$this->refreshFormData([
'calorie_calc' => 13
]);

#

no simple EditRecord

dire epoch
#

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);
    }

So if you pass in the correct attribute it should trump it...

try

$this->refreshFormData($this->record->refresh()->toArray())
prime dune
#

Method Filament\Forms\Form::refreshFormData does not exist.

dire epoch
#

Sorry updated

prime dune
#

$this->form->fill($this->record->refresh()->toArray()); this work correctly

#

I just wanted to understand if refreshFormData couldn't be used

dire epoch
#

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!