How the get the option label from a select

So, I've got a Select and I want to fill a TextInput with the value selected. Not the value of the option, but the label. Say, I've got
<select>
<option value="1">something</option>
</select>
<select>
<option value="1">something</option>
</select>
When the user selects something I want to fill a TextInput with something My Select looks like this:
Forms\Components\Select::make('exchange_rate_id')
->relationship('exchange_rate', 'rate')
->searchable()
->preload()
->live()
->reactive()
Forms\Components\Select::make('exchange_rate_id')
->relationship('exchange_rate', 'rate')
->searchable()
->preload()
->live()
->reactive()
and my TextInput:
Forms\Components\TextInput::make('exchange_rate')
->numeric()
->key('exchangeRate')
Forms\Components\TextInput::make('exchange_rate')
->numeric()
->key('exchangeRate')
I've tried using afterStateUpdated injecting the state, the component, Get, Set, but the only thing I can get is the option value (1 in this example). How can I get something from the selected option? Thanks in advance.
3 Replies
Tieme
Tieme•4mo ago
You can use Get or Set but need a model query to retrieve the label. when setting / getting
🤖transistor🤖
I thought that might be the case, but wanted to leave it as a last resort, to avoid more db calls Found the solution:
Forms\Components\Select::make('exchange_rate_id')
->relationship('exchange_rate', 'rate')
->searchable()
->preload()
->live()
->afterStateUpdated(function (Select $component, Set $set, string $state) {
$val = $component->getOptions();
$set('exchange_rate', $val[$state]);
}),
TextInput::make('exchange_rate')
->numeric()
->key('exchangeRate')
Forms\Components\Select::make('exchange_rate_id')
->relationship('exchange_rate', 'rate')
->searchable()
->preload()
->live()
->afterStateUpdated(function (Select $component, Set $set, string $state) {
$val = $component->getOptions();
$set('exchange_rate', $val[$state]);
}),
TextInput::make('exchange_rate')
->numeric()
->key('exchangeRate')
Tieme
Tieme•4mo ago
Nice, thanks for this code. I think i'm gonna use it to!