Make default select option other than the disabled option

how to make a default option other than the disabled option.
Select::make('status')
->options($this-options)
->default(array_key_first($this->options))
->disableOptionWhen(fn (string $value): bool => $value === 'option1')

$options = [
'option1' => 'Option 1', // suppose this is disabled option
'option2' => 'Option 2',
'option3' => 'Option 3',
'option4' => 'Option 4',
'option5' => 'Option 5',
];
Select::make('status')
->options($this-options)
->default(array_key_first($this->options))
->disableOptionWhen(fn (string $value): bool => $value === 'option1')

$options = [
'option1' => 'Option 1', // suppose this is disabled option
'option2' => 'Option 2',
'option3' => 'Option 3',
'option4' => 'Option 4',
'option5' => 'Option 5',
];
5 Replies
Patrick Boivin
Patrick Boivin15mo ago
Something like this?
->default(fn () => /* find the first option that is not disabled */)
->default(fn () => /* find the first option that is not disabled */)
ImShehryar
ImShehryarOP15mo ago
Thanks, but how to find the first option that is not disabled inside the options array and make it default.. As the options inside array are randomly disabled. for example:
->default(array_key_first($this->options->except('option which is disabled')) // like this or using collection to filter out options except the ones that are disabled
->default(array_key_first($this->options->except('option which is disabled')) // like this or using collection to filter out options except the ones that are disabled
Patrick Boivin
Patrick Boivin15mo ago
They are not randomly disabled in your example:
->disableOptionWhen(fn (string $value): bool => $value === 'option1')
->disableOptionWhen(fn (string $value): bool => $value === 'option1')
If you can share the actual code you're working with, it would be easier to help
ImShehryar
ImShehryarOP15mo ago
Thanks currently on mobile i will share later, is there any function to get the disabled options only: $component->getOptions() which gets all options, $component->getDisabledOptions() which can get the disabled options only
Patrick Boivin
Patrick Boivin15mo ago
I don't think there is a method like that but also I don't think you need it... in disableOptionWhen(), you basically decide which options should be disabled. Does that make sense? You can write a version of disableOptionWhen() and default() that takes this into account.
Want results from more Discord servers?
Add your server