Form field value based on 2 other fields

Docs show how to generate the value of a slug field. And shoes how to test against the old value of the field before changing it. How to fill the value of a field based on the values of 2 other fields? And only fill the third field if it is empty: don't change it if it already has value.
Solution:
Solved: My problem was in the Helper function. It wasn't returning....
Jump to solution
4 Replies
mmoollllee
mmoollllee4d ago
Can you provide a real example with the "other fields" to help you precicely?
Mohamed Ayaou
Mohamed Ayaou4d ago
You can just use a combination of Get, Set, and afterStateUpdated: this is an example from the docs: https://filamentphp.com/docs/3.x/forms/advanced#generating-a-slug-from-a-title
ambonboy.
ambonboy.OP4d ago
Here is the code I tried on the basis of the 'generating-a-slug-from-a-title' example in docs. The idea is that the field src_code will be generated by a helper function based on two other fields: leader, a string and execdir_id, an int. I don't know what is happening, but the first one works (leader) but nothing seems to happen when I change the execdir_id field (it is a select drop down). It eventually gives a max execution time exceeded error in the background.
->schema([
Forms\Components\TextInput::make('src_code')
Forms\Components\TextInput::make('leader')
->label('Group Leader')
->required()
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set) {
$src = Helpers::generateSrcCode($get('execdir_id'), $get('leader'));
$set('src_code', $src);
})
Forms\Components\Select::make('execdir_id')
->label('Executive Director of this group')
->options(function () {
return User::where('status', 'like', '%execdir%')->get()->pluck('name', 'id');
})
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set) {
$src = Helpers::generateSrcCode($get('execdir_id'), $get('leader'));
$set('src_code', $src);
})
->required()
->schema([
Forms\Components\TextInput::make('src_code')
Forms\Components\TextInput::make('leader')
->label('Group Leader')
->required()
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set) {
$src = Helpers::generateSrcCode($get('execdir_id'), $get('leader'));
$set('src_code', $src);
})
Forms\Components\Select::make('execdir_id')
->label('Executive Director of this group')
->options(function () {
return User::where('status', 'like', '%execdir%')->get()->pluck('name', 'id');
})
->live(onBlur: true)
->afterStateUpdated(function (Get $get, Set $set) {
$src = Helpers::generateSrcCode($get('execdir_id'), $get('leader'));
$set('src_code', $src);
})
->required()
Solution
ambonboy.
ambonboy.4d ago
Solved: My problem was in the Helper function. It wasn't returning.

Did you find this page helpful?