F
Filament4mo ago
Gaspar

enum value in Forms\Components\Select

My enum labels shows both as Select element value and label. Using Filament docs example here as a base.
enum Status: string implements HasLabel
{
case DRAFT = 'My draft label';
public function getLabel(): ?string
{
return $this->name;
}
}
enum Status: string implements HasLabel
{
case DRAFT = 'My draft label';
public function getLabel(): ?string
{
return $this->name;
}
}
Select::make('status')
->options(Status::class)
Select::make('status')
->options(Status::class)
It produces:
<option value="My draft label">My draft label</option>
<option value="My draft label">My draft label</option>
I'd like to have:
<option value="DRAFT">My draft label</option>
<option value="DRAFT">My draft label</option>
How to "fix" this?
6 Replies
Gaspar
GasparOP4mo ago
Thank you for reply. Yes i made this example based on Filament docs about Enums (my 2nd sentence in original post). So any idea why it is not working as i expect and docs say?
LeandroFerreira
LeandroFerreira4mo ago
case Draft = 'DRAFT';
...

public function getLabel(): ?string
{
return match ($this) {
self::Draft => 'My draft label',
...
};
}
case Draft = 'DRAFT';
...

public function getLabel(): ?string
{
return match ($this) {
self::Draft => 'My draft label',
...
};
}
Gaspar
GasparOP4mo ago
So i have to double my Enums with separate labels? I was really trying to avoid that.. Thank you!
Dennis Koch
Dennis Koch4mo ago
You really shouldn't use the value as a label. It's meant as a short descriptor for storing in the database etc.
Gaspar
GasparOP4mo ago
Understood. Thank you!
Want results from more Discord servers?
Add your server