Skip to content

[Bug]: Array to String Conversion Error in EditRecord Forms with Multiple/Range Date Picker #119

@sanchitspatil

Description

@sanchitspatil

What happened?

When using the Flatpickr component with multiplePicker() or rangePicker() in FilamentPHP 4 EditRecord forms, the component throws an "Array to string conversion" error during form hydration. The error occurs because FilamentPHP's DateTimeStateCast tries to process array data from the database as if it were a single datetime string.

The component works perfectly in CreateRecord forms but fails in EditRecord forms when existing array data is loaded from the database.

Environment

  • PHP Version: 8.3.25
  • Laravel Version: 12.29.0
  • FilamentPHP Version: 4.x

Error Stack Trace

ErrorException - Array to string conversion
vendor/filament/schemas/src/Components/StateCasts/DateTimeStateCast.php:42
vendor/filament/schemas/src/Components/Concerns/HasState.php:375
vendor/filament/filament/src/Resources/Pages/EditRecord.php:116

How to reproduce the bug

Set up a model with array-cast fields:

// In your model
protected $casts = [
    'dates' => 'array',
    'schedule_date_range' => 'array',
];

Create Flatpickr fields in your form:

Flatpickr::make('dates')->multiplePicker(),
Flatpickr::make('schedule_date_range')->rangePicker()

Save a record with multiple dates or date range

Try to edit the record - Error occurs during form hydration

Package Version

5.0.0

PHP Version

8.3.25

Laravel Version

12.29.0

Which operating systems does with happen with?

No response

Notes

Expected Behavior

The EditRecord form should load existing array data and display it properly in the Flatpickr components, just like CreateRecord forms work when saving array data.

Actual Behavior

  • CreateRecord: Works perfectly ✅
  • EditRecord: Throws "Array to string conversion" error ❌

The error happens because:

  1. EditRecord loads array data from database
  2. Flatpickr extends DateTimePicker which uses DateTimeStateCast
  3. DateTimeStateCast expects string/datetime values, not arrays
  4. Conversion fails before custom hydration logic can run

Proposed Solution
The core issue is that DateTimePicker's state casting runs before custom hydration logic.

  1. Override state casting order in the component's setUp() method
  2. Extend Field directly instead of DateTimePicker to avoid datetime state casting
  3. Add array data detection in the hydration process before parent casting occurs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions