formbuilderangular2-formbuilderangular-formbuilderformioflutter-form-builder

Making a field in formio calculated from other fields


Please I am using formio to create a form, within the form two fields (currency) are to be added and a total is to be generated in a field called total.

I have tried adding this custom javascript code in the calculated value section to the total field value= data.value1 + data.value2; I also tried value = parseInt(data.value1) + parseInt(data.value2);


Solution

  • I have run into this issue before. For some reason, setting the value does not display it in the field itself. What worked for me was using calculateValue by editing the JSON for the field. To do this:

    1. Click on the Edit JSON button for the field.

    2. Add the following code to add the values from the previous fields, where num1 and num2 are your unique keys:

      "calculateValue": {
        "+": [
          {
            "var": "data.num1"
          },
          {
            "var": "data.num2"
          }
        ]
      }
    

    1. The field should display the calculated value.

    References:

    https://jsonlogic.com/

    https://formio.github.io/formio.js/app/examples/calculated.html