Not able to select newly created options using CreateOptionsForm until refresh

Hey y'all, I've created some monstrosity. I'm sure there's a better way of doing this, and if you can help point me in the right direction I'd really appreciate it. I added CreateOptionsForms to each of the select fields, and they work, but the issue is that after creation, while I can see the option in the dropdown, if I try to actually select it it will pretend to let me for half a second, but then reset the select field to "Select an option". It's not actually selectable until I refresh the page. A second issue is, after creating the option, i'd like that new option to be automatically selected by the field. I've tried to accomplish this with the Product select but the approach I've taken just isn't doing anything. I'm imagining this is related to the first issue, the option not actually being selectable.
Solution:
I think you are supposed to return the id of the newly created model (as opposed to the model instance). Something like: ```php return \App\Models\Tos::create([ 'product_id' => $productId,...
Jump to solution
8 Replies
Solution
dissto
dissto7mo ago
I think you are supposed to return the id of the newly created model (as opposed to the model instance). Something like:
return \App\Models\Tos::create([
'product_id' => $productId,
'title' => $data['title'],
'company_id' => $companyId,
])->getKey(); // or ->id
return \App\Models\Tos::create([
'product_id' => $productId,
'title' => $data['title'],
'company_id' => $companyId,
])->getKey(); // or ->id
🤔
Vexmachina
VexmachinaOP7mo ago
That worked, thank you!
xechino
xechino5mo ago
Selecting the newly created model doesn't work when we use a model as a relationship instead of an array of options. Example
->relationship(
name: 'country',
titleAttribute: 'name',
)
->live()
->createOptionForm([
Forms\Components\TextInput::make('name')
])
->createOptionUsing(function (array $data, Forms\Set $set): string {
$country = Country::query()->create($data);
$set['country_id'] = $country->id;
return $country->id;
})
->relationship(
name: 'country',
titleAttribute: 'name',
)
->live()
->createOptionForm([
Forms\Components\TextInput::make('name')
])
->createOptionUsing(function (array $data, Forms\Set $set): string {
$country = Country::query()->create($data);
$set['country_id'] = $country->id;
return $country->id;
})
awcodes
awcodes5mo ago
CreateOptionUsing() is how to save the model. So you’re not actually saving the model. Based on what you showed. CreateOptionForm() will handle the saving automatically. Just depends on what you need.
xechino
xechino5mo ago
If I am creating the model with $country = Country::query()->create($data) The problem is that it is not selected, although I have seen that after creating another field in the same way, the previous one gets populated. The form code is this:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\Select::make('country_id')
->label('Country')
->relationship(
name: 'country',
titleAttribute: 'name',
)
->createOptionForm([
Forms\Components\TextInput::make('name'),
])
->createOptionUsing(function (array $data) {
$country = Country::query()->create($data);
// dd($country->uuid);
return $country->uuid;
})
->live()
->required(),
Forms\Components\Select::make('language_id')
->label('Language')
->relationship(
name: 'language',
titleAttribute: 'name',
)
->createOptionForm([
Forms\Components\TextInput::make('name'),
])
->createOptionUsing(function (array $data) {
$language = Language::create($data);
// dd($language->id);
return $language->id;
})
->live()
->required(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\Select::make('country_id')
->label('Country')
->relationship(
name: 'country',
titleAttribute: 'name',
)
->createOptionForm([
Forms\Components\TextInput::make('name'),
])
->createOptionUsing(function (array $data) {
$country = Country::query()->create($data);
// dd($country->uuid);
return $country->uuid;
})
->live()
->required(),
Forms\Components\Select::make('language_id')
->label('Language')
->relationship(
name: 'language',
titleAttribute: 'name',
)
->createOptionForm([
Forms\Components\TextInput::make('name'),
])
->createOptionUsing(function (array $data) {
$language = Language::create($data);
// dd($language->id);
return $language->id;
})
->live()
->required(),
]);
}
The Region model has these relationships:
public function country(): BelongsTo
{
return $this->belongsTo(Country::class, 'country_id');
}

public function language(): BelongsTo
{
return $this->belongsTo(Language::class, 'language_id');
}
public function country(): BelongsTo
{
return $this->belongsTo(Country::class, 'country_id');
}

public function language(): BelongsTo
{
return $this->belongsTo(Language::class, 'language_id');
}
No description
NickBell
NickBell5mo ago
@xechino did you manage to resolve this? I'm having the same issue and thinking of raising a bug as I don't believe this is the intended behaviour
xechino
xechino5mo ago
@NickBell, unfortunately, I haven't been able to resolve the issue yet. I've put it aside to revisit in the future, as I haven't had the time to create a new post or investigate further. If you do raise a bug report, please let me know how it goes!
arnaudsf
arnaudsf2mo ago
up people found a way to refresh the options in the select ? the only way I found to update the values is to have live() and preload() together.
Want results from more Discord servers?
Add your server