Set the HelperText attribute dynamically

Is it possible to dynamically change the HelperText of another field when changing the selected option of a Radiobutton? Not sure if I need to do this from the Radiobutton, or fetch the value from the field I want to adjust?
Radio::make('job_salary_type')
->columnSpanFull()
->inline()
->reactive()
->label(__('strings.fields.job_salary_type'))
->options([
'1' => __('strings.strings.monthly'),
'2' => __('strings.strings.yearly'),
])
->required()
->afterStateUpdated(function (Closure $set, $get, $state) {

// If the selected option is 1 then change the helperText to monthly
// If the selected option is 2 then change the helperText to yearly
$helperText = __('strings.strings.average_salary', ['type' => __('strings.strings.yearly')]);

// Set the helperText of the 'job_salary_avg' field
// But how?
$set('job_salary_avg', $helperText);
}),
Radio::make('job_salary_type')
->columnSpanFull()
->inline()
->reactive()
->label(__('strings.fields.job_salary_type'))
->options([
'1' => __('strings.strings.monthly'),
'2' => __('strings.strings.yearly'),
])
->required()
->afterStateUpdated(function (Closure $set, $get, $state) {

// If the selected option is 1 then change the helperText to monthly
// If the selected option is 2 then change the helperText to yearly
$helperText = __('strings.strings.average_salary', ['type' => __('strings.strings.yearly')]);

// Set the helperText of the 'job_salary_avg' field
// But how?
$set('job_salary_avg', $helperText);
}),
6 Replies
Dennis Koch
Dennis Koch2y ago
Instead of $set() you can use ->helperText(fn ($get) => ...)
Daniel Plomp
Daniel PlompOP2y ago
Thanks. Would it also fire when I change the Radio option?
Dennis Koch
Dennis Koch2y ago
Sure as long as the field is reactive
Daniel Plomp
Daniel PlompOP2y ago
OK, to be sure: I use this $get from my second field, to get the value of the radio button.
Dan Harrin
Dan Harrin2y ago
yes and the radio should be reactive
Daniel Plomp
Daniel PlompOP2y ago
OK! I'll try that. Thanks guys. Great, that works. 👍🏼

Did you find this page helpful?