F
Filament14mo ago
James

set default value for select

I cannot figure out how to set the default value for a select. I've tried both:
Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::DRAFT)
Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::DRAFT)
and
Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::DRAFT->value)
Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::DRAFT->value)
Neither seems to actually set the default value.
6 Replies
S. Mert ÖZTÜRK
S. Mert ÖZTÜRK14mo ago
First of all, you should use the value you gave to your options for the default selection. What does StatusEnum::DRAFT->value return? and StatusEnum::class
James
JamesOP14mo ago
It's an enum.
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum StatusEnum: string implements HasLabel
{
case DRAFT = 'draft';
case SCHEDULED = 'scheduled';
case PUBLISHED = 'published';

public function getLabel(): ?string
{
return match ($this) {
self::DRAFT => 'Draft',
self::SCHEDULED => 'Scheduled',
self::PUBLISHED => 'Published',
};
}
}
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;

enum StatusEnum: string implements HasLabel
{
case DRAFT = 'draft';
case SCHEDULED = 'scheduled';
case PUBLISHED = 'published';

public function getLabel(): ?string
{
return match ($this) {
self::DRAFT => 'Draft',
self::SCHEDULED => 'Scheduled',
self::PUBLISHED => 'Published',
};
}
}
S. Mert ÖZTÜRK
S. Mert ÖZTÜRK14mo ago
We can try this to determine where the error is.; Select::make('status') ->options([ 'draft' => 'Draft', 'scheduled' => 'Scheduled', 'published' => 'Published', ]) ->default('draft') Try this first and look for is it fixed. Apparently there is no error in the enum. You can try to dd(enum) for is it right return
James
JamesOP14mo ago
No that does not work either
S. Mert ÖZTÜRK
S. Mert ÖZTÜRK14mo ago
Actually, I have never tried to give a default value to select before. I did not need this as I filled out my form during the mount() phase of my Livewire component. If it's something you can do, move your form to a livewire component and assign a value to your select in mount(). I have no other advice to give in this situation.
LeandroFerreira
LeandroFerreira14mo ago
are you using on the create page or is it a custom page?
Want results from more Discord servers?
Add your server