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