How can I take the user-selected value in one form field for use in another field?

I would like to make the default() method dynamic by inserting a specific value based on the user's choice in the fk_servizio field, below is the code
Select::make('fk_servizio')
->label('ID Servizio')
->options(Servizi::all()->pluck('id', 'id'))
->searchable()
->required(),

TextInput::make('commissione')
->label('Importo')
->numeric()
->minValue(Servizi::where('id', ???)->value('amount'))
->default(Servizi::where('id', ???)->value('amount'))
->required(),
Select::make('fk_servizio')
->label('ID Servizio')
->options(Servizi::all()->pluck('id', 'id'))
->searchable()
->required(),

TextInput::make('commissione')
->label('Importo')
->numeric()
->minValue(Servizi::where('id', ???)->value('amount'))
->default(Servizi::where('id', ???)->value('amount'))
->required(),
6 Replies
wyChoong
wyChoong2y ago
read the doc https://filamentphp.com/docs/3.x/forms/advanced#injecting-the-state-of-another-field
Select::make('fk_servizio')
->label('ID Servizio')
->options(Servizi::all()->pluck('id', 'id'))
->reactive() //<- add this
->searchable()
->required(),

TextInput::make('commissione')
->label('Importo')
->numeric()
->minValue(fn($get) => Servizi::where('id', $get('fk_servizio'))->value('amount')) // <- changes
->default(fn($get) => Servizi::where('id', $get('fk_servizio'))->value('amount')) // <- changes
->required(),
Select::make('fk_servizio')
->label('ID Servizio')
->options(Servizi::all()->pluck('id', 'id'))
->reactive() //<- add this
->searchable()
->required(),

TextInput::make('commissione')
->label('Importo')
->numeric()
->minValue(fn($get) => Servizi::where('id', $get('fk_servizio'))->value('amount')) // <- changes
->default(fn($get) => Servizi::where('id', $get('fk_servizio'))->value('amount')) // <- changes
->required(),
_max28
_max28OP2y ago
this is exactly what i was looking for, now how could I set the value automatically? I saw that there is no value method
wyChoong
wyChoong2y ago
You have to understand what is the code doing and adjust accordingly. I’m just showing you how to get the value from another field and reactive field Maybe you should try $set
_max28
_max28OP2y ago
of course, I also looked at the documentation only that in v.2.x I can't find anything that says how to set the value field of the form
_max28
_max28OP2y ago
oh in the advanced section, alright thank you. I made it

Did you find this page helpful?