F
Filamentβ€’14mo ago
TranceCode

multiply 2 Input and show the result in a third input

Hey guys, i need multipley 2 input when i put the second value and show the result in other TextInput, in this case in imc i have my code like this: Forms\Components\TextInput::make('size') ->label('Altura') ->live() ->required(), Forms\Components\TextInput::make('weight') ->label('Peso Kg.') ->live() ->required(), Forms\Components\TextInput::make('imc') ->label('Indice de masa corporal') ->required(), in this case i need multiply size and weight and show the total in imc, but i need square size, when i put the value in weight and multiply, how can do this in Forms? Thank you guys!
4 Replies
Lara Zeus
Lara Zeusβ€’14mo ago
you can use $get and $set
TextInput::make('size')
->numeric()
->label('Altura')
->live()
->required(),
TextInput::make('weight')
->numeric()
->label('Peso Kg.')
->afterStateUpdated(fn(Set $set, Get $get)=>$set('imc',$get('size') * $get('weight')))
->live()
->required(),
TextInput::make('imc')
->label('Indice de masa corporal')
->live()
->required(),
TextInput::make('size')
->numeric()
->label('Altura')
->live()
->required(),
TextInput::make('weight')
->numeric()
->label('Peso Kg.')
->afterStateUpdated(fn(Set $set, Get $get)=>$set('imc',$get('size') * $get('weight')))
->live()
->required(),
TextInput::make('imc')
->label('Indice de masa corporal')
->live()
->required(),
TranceCode
TranceCodeOPβ€’14mo ago
Its working @Lara Zeus thank you for the tips... work really cool i add a debounce only for fix a problem if any add 2 or more number in weight, and this is the code: Forms\Components\TextInput::make('weight') ->label('Peso Kg.') ->numeric() ->live(debounce: 1500) ->afterStateUpdated(fn(Set $set, Get $get)=>$set('imc', $get('weight') / ($get('size') * $get('size')) )) ->required(),
Lara Zeus
Lara Zeusβ€’14mo ago
that prefect also you can use ->live(onBlur: true) to only update when the user "leave" the input πŸ™‚
TranceCode
TranceCodeOPβ€’14mo ago
i will test it, thank you bro!
Want results from more Discord servers?
Add your server