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
toeknee2y ago
are you editing a form you have added this field too by any chance?
josef
josefOP2y ago
yes
toeknee
toeknee2y ago
That will be why since the field is now empty. IF you create a new record, does it work as normal?
josef
josefOP2y ago
ah yes, you're right. On create, it works as expected, but not on edit
josef
josefOP2y ago
but why is the field empty? shouldn't it use the value I have set on the model/db?
LeandroFerreira
currency? The field name is "curency"..
toeknee
toeknee2y 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
josefOP2y ago
thanks, fixed the name and suddenly it works 😅
toeknee
toeknee2y ago
Magic
LeandroFerreira
I know exactly how that feels! Hahaha

Did you find this page helpful?