using ENUM class on Forms->Select Field return error

I get the follwing error trying to use an ENUM on a SelectField: Filament\Forms\Components\Select::getOptions(): Return value must be of type array, string returned my enum:
namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum Durations: string implements HasLabel
{
case OneDay = 'one_day';
case TwoDays = 'two_days';
case ThreeDays = 'three_days';
case FourDays = 'four_days';

public function getLabel(): ?string
{

return match ($this) {
self::OneDay => 'Ein Tag',
self::TwoDays => 'Zwei Tage',
self::ThreeDays => 'Drei Tage',
self::FourDays => 'Vier Tage',
};
}
}
namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum Durations: string implements HasLabel
{
case OneDay = 'one_day';
case TwoDays = 'two_days';
case ThreeDays = 'three_days';
case FourDays = 'four_days';

public function getLabel(): ?string
{

return match ($this) {
self::OneDay => 'Ein Tag',
self::TwoDays => 'Zwei Tage',
self::ThreeDays => 'Drei Tage',
self::FourDays => 'Vier Tage',
};
}
}
My cast:
'duration' => Durations::class,
'duration' => Durations::class,
My SelectField in Forms:
Forms\Components\Select::make('duration')
->label('Dauer')
->options(Durations::class)
->required()
->reactive(),
Forms\Components\Select::make('duration')
->label('Dauer')
->options(Durations::class)
->required()
->reactive(),
What am i missing? php 8.2
5 Replies
cheesegrits
cheesegrits2y ago
That should work, I don't see anything wrong. I literally just now implemented an enum with a Select, and compared your code to mine. Too tired to delve into it now tho. Will check back after some zzz zzz.
CodeMax
CodeMaxOP2y ago
Im pretty sure it worked the whole time. Can you try updating composer dependencies to the latest versions of filament pls?
cheesegrits
cheesegrits2y ago
I'm definitely on latest (3.0.62). Schema ...
Forms\Components\Select::make('type')
->options(EngagementType::class)
->required()
->reactive(),
Forms\Components\Select::make('type')
->options(EngagementType::class)
->required()
->reactive(),
Model ...
protected $casts = [
'type' => EngagementType::class,
'status' => EngagementStatus::class,
];
protected $casts = [
'type' => EngagementType::class,
'status' => EngagementStatus::class,
];
Enum ...
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum EngagementType:string implements HasLabel, HasIcon
{
case Phone = 'phone';

case Email = 'email';

case Text = 'text';

case Letter = 'letter';

case Chat = 'chat';

public function getLabel(): ?string
{
return $this->name;
}

public function getIcon(): ?string
{
return match ($this) {
self::Phone => 'heroicon-m-phone',
self::Email => 'heroicon-m-at-symbol',
self::Text => 'heroicon-m-device-phone-mobile',
self::Letter => 'heroicon-m-envelope',
self::Chat => 'heroicon-m-chat-bubble-bottom-center-text',
};
}
}
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasIcon;
use Filament\Support\Contracts\HasLabel;

enum EngagementType:string implements HasLabel, HasIcon
{
case Phone = 'phone';

case Email = 'email';

case Text = 'text';

case Letter = 'letter';

case Chat = 'chat';

public function getLabel(): ?string
{
return $this->name;
}

public function getIcon(): ?string
{
return match ($this) {
self::Phone => 'heroicon-m-phone',
self::Email => 'heroicon-m-at-symbol',
self::Text => 'heroicon-m-device-phone-mobile',
self::Letter => 'heroicon-m-envelope',
self::Chat => 'heroicon-m-chat-bubble-bottom-center-text',
};
}
}
cheesegrits
cheesegrits2y ago
No description
DrByte
DrByte2y ago
Filament\Forms\Components\Select::getOptions(): Return value must be of type array, string returned
I get that whenever I forget to import my Enum into my Form class's file headers. ie: use \App\Enums\Durations; or use the FQDN: ->options(\App\Enums\Durations::class)

Did you find this page helpful?