filament icon indicating copy to clipboard operation
filament copied to clipboard

Default values not selected in multiple select input on page load

Open SiavashBamshadnia opened this issue 1 year ago • 4 comments

Package

filament/filament

Package Version

v3.2.123

Laravel Version

v11.30.0

Livewire Version

v3.5.12

PHP Version

8.2

Problem description

I have a simple multiple select input where the default values are not selected on page load.

Here is the form function in my UserResource.php file:

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Select::make('dummy select')
                ->options([1, 2, 3, 4])
                ->default([1, 3])
                ->multiple()
        ]);
}

As you can see, it’s a straightforward setup with no complex logic, but the default values are not being pre-selected as expected.

Expected behavior

The select element should load with the default values [1, 4] pre-selected.

Steps to reproduce

  1. Create a form with a multiple select input using the provided code.
  2. Load the form page and observe that the default values [1, 4] are not pre-selected in the select input.

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

https://github.com/SiavashBamshadnia/filament_default_select_problem

Relevant log output

No response

SiavashBamshadnia avatar Nov 10 '24 18:11 SiavashBamshadnia

in your code the default value is 1,3 do you mean default value should be 1 and 3

th pre selected value working fine

image

you should set the options based key => value like this

[ 
   0 => 1, 
   1 => 2, 
   2 => 3,
   3 => 4 
]

if you want take 4 as default value so set the defaultValue to 3 ( index of 4 value)

more example

public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('language')
                    ->options([
                        'php' => 'PHP',
                        'javascript' => 'Javascript',
                        'java' => 'JAVA'
                    ])
                    ->default(['php','java']) // set php and java as default value
                    ->multiple()
            ]);
    }

ahmadrivaldi-arv avatar Nov 12 '24 11:11 ahmadrivaldi-arv

I have the same issue:

 Forms\Components\Select::make('options')
                    ->label('Products')
                    ->required()
                    ->multiple()
                    ->options(Product::pluck('name', 'id')->toArray())
                    ->columnSpanFull(),

In db it saves the data correctly: ["4", "6", "8", "9", "10", "11"]

But If I want to edit I don't see the selected elements, why?

zsoltjanes avatar Dec 01 '24 12:12 zsoltjanes

I agree with @ahmadrivaldi-arv in https://github.com/filamentphp/filament/issues/14757#issuecomment-2470318226

@zsoltjanes, if you use multiple(), you need to cast your attribute to json or array in your model.

    protected $casts = [
        'options' => 'json'
    ];

There's no bug here, @danharrin.

borjajimnz avatar Jan 04 '25 22:01 borjajimnz

@borjajimnz I already cast it to array :/

zsoltjanes avatar Jan 04 '25 23:01 zsoltjanes

Thanks @borjajimnz, I also can't reproduce this issue

danharrin avatar Jun 22 '25 15:06 danharrin