formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Calculate Value going on an infinte loop when setting a value from API response

Open Vedhachala opened this issue 7 years ago • 3 comments

Hi, I am trying to assign API response to value using instance.setValue as suggested in this issue, https://github.com/formio/formio.js/issues/915 i am observing the code in the callback function is going in a infinite loop.

Note: Similar infinite loop behavior is observed in calculate value field while using instance.setValue(data) without any api call.

I feel instance.setValue is modifying the submission data object which triggers a change in the form and calls calculate value again by itself and hence the infinite loop.

How to make this work ?

Vedhachala avatar Apr 05 '19 08:04 Vedhachala

Will you provide a jsfiddle of this happening? Or maybe the JSON of the form that has the problem.

travist avatar Apr 05 '19 14:04 travist

I am facing this issue in form builder .

Steps: Add a textfield and navigate to calculate value.The content of calculate Value filed is : fetch('https://www.mocky.io/v2/5185415ba171ea3a00704eed') .then(function(response) { console.log('test'); instance.setValue(JSON.stringify(response)); }); I can observe the network calls and the console output is logging repeatedly.

@travist i dont observe this behavior if i use this json to render it in form renderer directly.

Vedhachala avatar Apr 09 '19 05:04 Vedhachala

The calculate value would re-trigger on "setValue" so that is what is causing the infinite loop. Try adding a "changed" protection around your calculate value.

// Only set the value if it changes.
response = JSON.stringify(response);
if (instance.dataValue !== response) {
  instance.setValue(response);
}

travist avatar Apr 09 '19 21:04 travist