How can i add select option dynamically from suffixAction ?

I have a select field with one option initially. I have also added a suffixAction to pick a record and trying to add new option from picked record dynamically to select field. but can't do it. can anyone help please?
Forms\Components\Select::make('union_id')
->label(__('labels.union'))
->options(function (): array {
$union = Union::find(auth()->user()->location[0]);
return [
$union->id => $union->name_bn
];
})
->suffixAction(
Forms\Components\Actions\Action::make("select_union")
->icon('heroicon-o-plus')
->modalHeading(__('labels.select_union'))
->form(self::locationForm())
->modalCancelAction(false)
->modalSubmitActionLabel(__('labels.select'))
->action(function (Set $set, $state, array $data, Component $component) {

$union = Union::find($data['union']);
$component->options([
$union->id => $union->name_bn
]);
$set('union_id', $union->id);

}),
)
Forms\Components\Select::make('union_id')
->label(__('labels.union'))
->options(function (): array {
$union = Union::find(auth()->user()->location[0]);
return [
$union->id => $union->name_bn
];
})
->suffixAction(
Forms\Components\Actions\Action::make("select_union")
->icon('heroicon-o-plus')
->modalHeading(__('labels.select_union'))
->form(self::locationForm())
->modalCancelAction(false)
->modalSubmitActionLabel(__('labels.select'))
->action(function (Set $set, $state, array $data, Component $component) {

$union = Union::find($data['union']);
$component->options([
$union->id => $union->name_bn
]);
$set('union_id', $union->id);

}),
)
4 Replies
toeknee
toeknee15mo ago
You are in an action so I suspect you need to get out of the action try/: $set('../../union_id', 702);
Arif Hossain
Arif HossainOP15mo ago
thanks. $set('union_id', $union->id) is working but new union is not adding to option that is my problem.
toeknee
toeknee15mo ago
Why not use a select with a create form? You'd need to check how we do it there and reverse it back if it's possible
Arif Hossain
Arif HossainOP15mo ago
create form is creating a option of a relationship select. but I'm trying to pick an option and my select field has no relationship.
Want results from more Discord servers?
Add your server