filament icon indicating copy to clipboard operation
filament copied to clipboard

Calling refreshFormData on repeat fields clears them

Open euoia opened this issue 1 year ago • 3 comments

Package

filament/filament

Package Version

v3.2.110

Laravel Version

v11.22.0

Livewire Version

v3.5.6

PHP Version

PHP 8.3.8

Problem description

I called refreshFormData with fields fields that are defined using Repeater::make and it cleared them.

Expected behavior

I expected the fields to be updated with their latest values.

Steps to reproduce

  1. Create a simple Repeater field in your Resource:
                Repeater::make('comments')
                    ->simple(
                        TextInput::make('comment'),
                    ),
  1. Add an action to getHeaderActions in the EditRecord:
            Actions\Action::make('update comments')
                ->action(function () {
                    // Modify comments
                    $this->record->comments = ['foo', 'bar'];
                    $this->record->save();

                    $this->refreshFormData([
                        'comments',
                    ]);
                }),
  1. Observe that the comments repeater fields are cleared instead of updated with new values.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/cloudshipco/filamentphp-repeater-refresh-bug

Relevant log output

n/a

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar

euoia avatar Sep 12 '24 13:09 euoia

In that scenario I would typically use $this->fillForm() instead of $this->refreshFormData().

Otherwise, looking at your form schema, the refreshFormData would typically look for an array of comments with each having a comment key corresponding to the name of the TextInput. So this change should work:

- $this->record->comments = ['foo', 'bar'];
+ $this->record->comments = [['comment' => 'foo'], ['comment' => 'bar']];

yuters avatar Sep 13 '24 15:09 yuters

@yuters, simple repeaters do actually use a flat array to store items, so this might in fact be a bug.

zepfietje avatar Sep 26 '24 10:09 zepfietje

@zepfietje yep could probably be improved for simple repeaters. Just suggesting some workarounds.

yuters avatar Sep 26 '24 12:09 yuters

Should be fixed by #15514

danharrin avatar Feb 06 '25 11:02 danharrin