F
Filament13mo ago
Omar

Set default value to a select, listening the change from another input

Forms\Components\DatePicker::make('extinguisher_expiration')->live(debounce: 500),
Forms\Components\Select::make('extinguisher_status')
->options([
'true' => 'true',
'false' => 'false',
])
->default(function(Get $get){
if(!is_null($get('extinguisher_expiration'))){
return 'true';
}
return 'false';
}),
Forms\Components\DatePicker::make('extinguisher_expiration')->live(debounce: 500),
Forms\Components\Select::make('extinguisher_status')
->options([
'true' => 'true',
'false' => 'false',
])
->default(function(Get $get){
if(!is_null($get('extinguisher_expiration'))){
return 'true';
}
return 'false';
}),
I am trying to assign a default value to the select, which would be true when having a data and false when not having it, but it does not detect the change of the input date
3 Replies
Andrew Wallo
Andrew Wallo13mo ago
Use afterStateUpdated() on the Date picker field, then use $set(‘extinguisher_status’, whatever_here) instead. Like this:
Forms\Components\DatePicker::make('extinguisher_expiration')
->live(debounce: 500)
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('extinguisher_status', true);
}
}),
Forms\Components\Select::make('extinguisher_status')
->options([
'true' => 'true',
'false' => 'false',
])
->default('false'),
Forms\Components\DatePicker::make('extinguisher_expiration')
->live(debounce: 500)
->afterStateUpdated(function (Set $set, $state) {
if ($state) {
$set('extinguisher_status', true);
}
}),
Forms\Components\Select::make('extinguisher_status')
->options([
'true' => 'true',
'false' => 'false',
])
->default('false'),
Let me know if that works for you.
Omar
OmarOP13mo ago
It works, thank you very much for the information
Andrew Wallo
Andrew Wallo13mo ago
Your welcome

Did you find this page helpful?