Relationship select with grouped options and create action.
On my relationship-select I'm using the
options
method for grouping the Entries. Also I'm using the createOptionForm
for adding the ability to create new Entries.
Problem: After adding the new Entry the value of the select is set to the new option, but the Option is not listed with the other options.
When I remove the options
it works as expected.
Forms\Components\Select::make('event_type_id')
->relationship('eventType', 'name')
->native(false)
->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys())
->createOptionForm([
Forms\Components\Grid::make()
->schema([
EventTypeResource::metaTypeField()
->required(),
EventTypeResource::nameField()
->required(),
EventTypeResource::descriptionField()
->columnSpanFull(),
]),
])
->required();
Forms\Components\Select::make('event_type_id')
->relationship('eventType', 'name')
->native(false)
->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys())
->createOptionForm([
Forms\Components\Grid::make()
->schema([
EventTypeResource::metaTypeField()
->required(),
EventTypeResource::nameField()
->required(),
EventTypeResource::descriptionField()
->columnSpanFull(),
]),
])
->required();
1 Reply
I already tried the following:
Forms\Components\Select::make('event_type_id')
// ...
->live()
->afterStateUpdated(fn (Forms\Components\Select $component) => $component
->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys()))
Forms\Components\Select::make('event_type_id')
// ...
->live()
->afterStateUpdated(fn (Forms\Components\Select $component) => $component
->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys()))
Forms\Components\Select::make('event_type_id')
// ...
->createOptionAction(fn (Forms\Components\Actions\Action $action) => $action
->after(function (Forms\Components\Select $component) {
$component->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys()));
}))
Forms\Components\Select::make('event_type_id')
// ...
->createOptionAction(fn (Forms\Components\Actions\Action $action) => $action
->after(function (Forms\Components\Select $component) {
$component->options(EventType::all()
->groupBy(fn(EventType $eventType) => $eventType->meta_type->getLabel())
->map(fn(Collection $group) => $group
->pluck('name', 'id')
->sort())
->sortKeys()));
}))