Populate Select with only a select subset of an enum
What I am trying to achieve is populating the Select with only certain values of an Enum based on permissions. It is likely I am approaching this entirely the wrong way, and have tried various solutions without success.
Although this does not work, it shows what I am trying to achieve. The ArticleStatus is a backed Enum and the value of that enum is stored in the status attribute (it is casted).
Everything works properly if I want to load all the values of the enum (
->options(ArticleStatus::class)
).
Running the code below gives this error: Filament\Forms\Components\Select::isOptionDisabled(): Argument #2 ($label) must be of type string, App\Enums\ArticleStatus given
Solution:Jump to solution
So you could map the allowed enum cases to key/value pairs (enum value as key, and label as value) for ->options/icons/colors:
```php
Select::make('status')
->selectablePlaceholder(false)...
4 Replies
So I made a similar post about this here:
https://discord.com/channels/883083792112300104/1353819807941660723/1353819807941660723
For me, to get around the error naively I did something like this (note here i'm just rejecting a single case):
In that case i also wanted it to render icons and colors from the enum, so i disgustingly got it working like so:
So under the same logic, you could just wrap the result of the permission check in a collection before converting it to the array:
Solution
So you could map the allowed enum cases to key/value pairs (enum value as key, and label as value) for ->options/icons/colors:
Just doesn't seem too DRY for my liking, unless you make some sort of macro, or some helper. Gonna eventually dive into options() to improve this but gotta move on to the next feature now lol.
@Ash Thank you! Keywords I was searching did not bring up yours unfortunately.