F
Filamentβ€’2y ago
josef

Default value for select

I want to have the default value for a select not only pre-selected in the frontend, but also when saving. Currently, the validation throws an error that a value has to be chosen, even though the select (with placeholder disabled) shows the value that should be default. An example:
Forms\Components\Select::make('curency')
->required()
->options([
'EUR' => '€',
'USD' => '$',
])
->default('EUR')
->disablePlaceholderSelection(),
Forms\Components\Select::make('curency')
->required()
->options([
'EUR' => '€',
'USD' => '$',
])
->default('EUR')
->disablePlaceholderSelection(),
10 Replies
toeknee
toekneeβ€’2y ago
are you editing a form you have added this field too by any chance?
josef
josefOPβ€’2y ago
yes
toeknee
toekneeβ€’2y ago
That will be why since the field is now empty. IF you create a new record, does it work as normal?
josef
josefOPβ€’2y ago
ah yes, you're right. On create, it works as expected, but not on edit
josef
josefOPβ€’2y ago
but why is the field empty? shouldn't it use the value I have set on the model/db?
LeandroFerreira
LeandroFerreiraβ€’2y ago
currency? The field name is "curency"..
toeknee
toekneeβ€’2y ago
The field is empty because you pass in an empty variable on the mounting of the data as we empty the dataset then push in the new data. What you should do, manipulate the data if it is null/empty then return a default value.
josef
josefOPβ€’2y ago
thanks, fixed the name and suddenly it works πŸ˜…
toeknee
toekneeβ€’2y ago
Magic
LeandroFerreira
LeandroFerreiraβ€’2y ago
I know exactly how that feels! Hahaha
Want results from more Discord servers?
Add your server