Default values not selected in multiple select input on page load
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
- Create a form with a multiple select input using the provided code.
- 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
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
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()
]);
}
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?
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 I already cast it to array :/
Thanks @borjajimnz, I also can't reproduce this issue