Dependant fields in Form

I have a form that has 2 select fields and a number* input field. The number field depends on the selected values from the 2 other fields. The value of the number field is calculated via a variable (tried also with a transformer code).

When either of the select fields is updated the variable gets the value but the number field get the value from the previous selection; it is like the refresh of the number field happens before the events of updating either of the select fields completes.

Tried also with a text input field and had the same behaviour.

Any suggestions on how to make sure the number field has the value of the variable once the select fields are updated?

Example:
If Field 1 = A and Field 2 = B then Field 3 should be XXX
If Field 1 = P and Field 2 = Q then Field 3 should be YYY

If I select A and B then the value is empty
If I select P and Q then the value is XXX
If I clear Field 1 and 2 then the value is YYY

Thanks

Do you really need to make it an input if the value is calculated based on the other fields? I would switch it to a text component and just show the value of the variable (or if the user entering the info in the form doesn't actually need to see it, just use the variable in the query that you use when the form is submitted).

@jg80 I wish. Depending of the combination the field is disabled or not. sometimes the user can update this number field.
I know that if I don't find a solution I will need to have 2 fields, one text and one input number to hide the one we don't need.
I only wanted to see why this is happening and understand for future development. I assume the refresh of the input number field is done before the calculation of the value so the new value is only shown on the next refresh.

To add to the issue, the number field is required... so if I use the idea of having the number input when the user cannot update and the text when the number is fixed, the required validation doesn't happen. I added the same logic to hide the number format field to the required field (reverse) but seems again and it doesn't auto-validate and set the required text when the number input is empty.
Will continue trying and report back what I am able to figure out. I don't believe that I am the first person with this issue

Got it. Do you have the "default value" of the number input set to variable.value? When I do this, I haven't encountered a situation where the default value of the field is disconnected from the variable value. Is the variable value being set by a JS query that is triggered each time one of the inputs to the calculation of the variable is changed? I've run into situations in my own development where I was missing a trigger, so it looked like the value was lagging the selections because it wasn't actually updating until the next step changed...

@jg80 thanks for your input. I have tried everything; default field value, variables, transformers, queries...

My conclusion is that the refresh of the number input (or text input for that matter) happens before the trigger of the calculation even though the calculation has been placed everywhere (success of a query, select change event, etc.).

I decided to go to what the solution I was avoiding: having the number input visible when the value can be updated and the text with the variable (calculated) when the value cannot be updated. When I disconnect everything from the number input field (no triggers, no defaults, no nothing) and only having a fx for the required field validation, then everything works.

My calculation is smart enough to have always on the variable the calculated value when needed or the number input when updated by the user.

Still don't understand why the value of the variable after the calculation is not places on the number input... but I have to move on!

And my calculation wasn't too complicated:

image

Oh well! Thanks again @jg80

:person_shrugging: well, I guess as long as you can make it work…maybe there is a “reset state” or a “clear value” on the variable that is needed somewhere along the way, or maybe it just is the order in which things execute. Frustrating not to have a definitive answer, but that’s for sharing with the community so others who run into something like this have a resource!

2 Likes

You are right! What puzzles me is that the variable ALWAYS have the correct value no matter what. I guess that is a good thing because I am using it to save the record.