platform icon indicating copy to clipboard operation
platform copied to clipboard

Error with enum cast and select field

Open keasy9 opened this issue 1 month ago • 2 comments

If model field casted as enum class, e.g.:

protected $casts = [
    'user_type' => UserTypeEnum::class,
];

and select field is used to edit it:

Select::make('user_type')->options(
    collect(UserTypeEnum::cases())
        ->mapWithKeys(fn($case) => [$case->value => $case->title()])
),

fatal php error is appearing: Image

keasy9 avatar Nov 10 '25 06:11 keasy9

Could you please explain why options is used here when the value isn’t an array? It seems more logical to use options() for arrays and fromEnum() for enum values.

https://github.com/orchidsoftware/platform/blob/2c3cc1be7fb9b1c773187479935dbea945dd3e6b/tests/Unit/Screen/Fields/SelectTest.php#L271-L302

tabuna avatar Nov 10 '25 08:11 tabuna

Oh, forgot about fromEnum method, thanks! But shouldn't code works even with options? Shouldn't that:

Select::make()->value('some str value')->options([
    'some str value' => 'some str title',
    'some other str value' => 'some other str value',
]),

be equal to:

Select::make()->value(StringEnum::someCase)->options([
    StringEnum::someCase->value => 'some str title',
    StringEnum::someOtherCase->value => 'some other str value',
]),

and work in both ways?

keasy9 avatar Nov 17 '25 08:11 keasy9