filament/filament
v3.2.128
v11.34.2
v3.5.12
8.3.12
When using a Select, and passing an array of groups to options()
, numeric keys (representing database IDs) are discarded when getting the enabled options via $component->getEnabledOptions()
.
public function getEnabledOptions(): array
{
return collect($this->getOptions())
->reduce(function (Collection $carry, $label, $value): Collection {
if (is_array($label)) {
return $carry->merge($label); // <= this discards numeric keys
}
return $carry->put($value, $label);
}, collect())
->filter(fn ($label, $value) => ! $this->isOptionDisabled($value, $label))
->all();
}
Numeric keys are kept, flattening the options array like so:
public function getEnabledOptions(): array
{
return collect($this->getOptions())
->reduce(function (Collection $carry, $label, $value): Collection {
if (is_iterable($label)) {
foreach ($label as $k => $v) {
$carry->put($k, $v);
}
return $carry;
}
return $carry->put($value, $label);
}, collect())
->filter(fn ($label, $value) => ! $this->isOptionDisabled($value, $label))
->all();
}
Having the following schema:
return $form->schema([
Forms\Components\Select::make('foobar')
->options([
'Foo' => [
10 => 'Option 10',
],
'Bar' => [
20 => 'Option 20',
],
])
->in(function (Select $component): array {
dd($component->getEnabledOptions());
})
->required(),
]);
This is the result of the dd:
array:2 [▼ // app/Filament/Resources/PageResource.php:37
0 => "Option 10"
1 => "Option 20"
]
While I expected this result:
array:2 [▼ // app/Filament/Resources/PageResource.php:35
10 => "Option 10"
20 => "Option 20"
]
https://github.com/standaniels/filament-bug
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too