Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Set Input Value on Select Change

2024-05-03

If you want to set the text input value based on the changed Select option, you can do it with afterStateUpdated() and $set.

Part of the form() method:

->schema([
Forms\Components\Select::make('product_id')
->relationship('product', 'name')
->live()
->afterStateUpdated(function (Get $get, Set $set) {
$set('price', Product::find($get('product_id'))->price);
})
->required(),
Forms\Components\TextInput::make('price')
->required()
->numeric()
->prefix('$'),

In the afterStateUpdated() method, you get all the form current values in $get(fieldName), and then set the value of any other field with $set(fieldName, newValue).

This snippet comes from a bigger premium example CRM: Customer Management System

A few of our Premium Examples: