filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
17.15k stars 2.69k forks source link

EditRecord's refreshFormData method does not work with DateTimePicker #13232

Open 3rgo opened 2 months ago

3rgo commented 2 months ago

Package

filament/filament

Package Version

v3.2.90

Laravel Version

v10.48.12 (also applies to v11.10.0 as shown by the reproduction repository)

Livewire Version

No response

PHP Version

8.3.7

Problem description

On an EditRecord page for my resource, I added 2 header actions that update a datetime field in my Model (set as datetime cast with no mutator/accessor).

This field is displayed using the Forms\Components\DateTimePicker component, without any extra logic.

When I call the refreshFormData method in my action ($this->refreshFormData(['registration_end']);, the DateTimePicker's value is cleared even though a value is set in the database.

If I change the field type to TextInput, the update is shown immediately.

Expected behavior

The DateTimePicker field value should be updated according to new model value, without having to refresh the page

Steps to reproduce

To confirm, change field type to TextInput and repeat action calls, refresh will be performed properly

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

https://github.com/3rgo/filament-refreshformdata-datetimepicker

Relevant log output

No response

Donate 💰 to fund this issue

Fund with Polar

github-actions[bot] commented 2 months ago

Hey @3rgo! We're sorry to hear that you've hit this issue. 💛

However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue?

We need a public GitHub repository which contains a Laravel app with the minimal amount of Filament code to reproduce the problem. Please do not link to your actual project, what we need instead is a minimal reproduction in a fresh project without any unnecessary code. This means it doesn't matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly.

3rgo commented 2 months ago

WTH, the reproduction repository URL is filled (and was filled on the initial issue submission, I didn't have to edit)... @danharrin I think the bot is broken 😅

danharrin commented 2 months ago

Thanks

websmithcode commented 1 month ago

To solve this - i do replace:

->action(function (Proxy $record, EditProxy $livewire, Forms\Components\DateTimePicker $component) {
    $record->recheck(update: true);
    $livewire->refreshFormData(['broken_at']);
})

with

->action(function (Proxy $record, EditProxy $livewire, Forms\Components\DateTimePicker $component) {
    $record->recheck(update: true);
    if ($record->broken_at) {
        $livewire->data['broken_at'] = $record->broken_at->format($component->getFormat());
    } else {
        $livewire->data['broken_at'] = null;
    }
})

Just check file vendor/filament/filament/src/Resources/Pages/EditRecord.php on 121 line:

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

It's not handle casts and updates data "As is"