FilamentF
Filament3y ago
8 replies
Josh777

Why Select field does not save as null when no options exist i.e. only placeholder

I have a problem where I have a Select with dynamic options generated within a repeater.

If the Select has options and the user has saved the form with an option selected, the data is submitted to the json field in the database - as expected.

If the Select no longer has any options i.e. an empty state with the placeholder "Select an option", this should save a null value back to the database but it doesn't.

It still has the data attached that existed before, even though the Select is empty.

I can prove this by dumping $data within mutateFormDataBeforeSave(array $data), the field has the data from before and I am very confused as to why.

It is my understanding $data includes client side submitted values only and does not fetch anything from the database at this stage.

So why would it be fetching the previous data from the Select that no longer exists?

Steps:

Before:
Repeater::make('maintenance')
    ->schema([
        Select::make('type')
            ->label('Maintenance Type')
            ->options(['water' => 'Water', 'filter' => 'Filter'])
        ]);


User selects 'water', then value is saved to database in json e.g. "type": "water"

After:
Repeater::make('maintenance')
    ->schema([
        Select::make('type')
            ->label('Maintenance Type')
            ->options([])
        ]);


User cannot select anything, submits form and in the $data array, "water" still exists within the submitted data. How?!
Was this page helpful?