Fill form data when a specific field changes

Hello, I have this form:
$form->schema([
Forms\Components\TextInput::make('hourly_price')
->label(trans('admin.room.hourly_price'))
->numeric()
->default(0)
->required(),
Forms\Components\TextInput::make('daily_price')
->label(trans('admin.room.daily_price'))
->numeric()
->default(0)
->required(),
Forms\Components\TextInput::make('monthly_price')
->label(trans('admin.room.monthly_price'))
->numeric()
->default(0)
->required(),
]);
$form->schema([
Forms\Components\TextInput::make('hourly_price')
->label(trans('admin.room.hourly_price'))
->numeric()
->default(0)
->required(),
Forms\Components\TextInput::make('daily_price')
->label(trans('admin.room.daily_price'))
->numeric()
->default(0)
->required(),
Forms\Components\TextInput::make('monthly_price')
->label(trans('admin.room.monthly_price'))
->numeric()
->default(0)
->required(),
]);
I want when hourly_price value changes I calculate the daily_price and monthly_price and fill them automatically. How can I implement this? I tried using afterStateUpdated but it seems that I cannot access all the form data and manipulate the values.
7 Replies
Tin
Tin16mo ago
In your afterStateUpdated use Set $set to change the value of other fields
awcodes
awcodes16mo ago
You will also need to make ‘hourly_price’ ->reactive()
SEMICOLON;
SEMICOLON;16mo ago
Can I access the value daily_price inside TextInput::make('hourly_price')->afterStateUpdated(...) ? is seems that state returns only the value hourly_price
TextInput::make('hourly_price')->afterStateUpdated(function (Closure $set, $state) {
dd($state);
})
TextInput::make('hourly_price')->afterStateUpdated(function (Closure $set, $state) {
dd($state);
})
state returns only the input of hourly_price
Tin
Tin16mo ago
use $get closure
awcodes
awcodes16mo ago
$state only applies to that specific field. So you use $get and $set to manipulate other field values.
SEMICOLON;
SEMICOLON;16mo ago
Thank you so much guys 👍
Want results from more Discord servers?
Add your server