How can manage calculation values and validation in one component ?
i'm using formio from four months at beginning forms was more simple , at this time the forms more complex (more than 30 text filed ) there are calculating values in each field so i have to edit each component and add some calculating smoe cases (call rest api) , it's possible manage the calculation in on component?
Fox example : i have 4 numbers fields and total filed and message field, there are calculating value in (total, message total) it's possible write these calculation in one place (general component custom or any similar to do this) and update in calculating or validation change in one place .
See the below link for examlple : https://drive.google.com/file/d/1_x1rWY33KSOSMc7IfZ9V880L68f-hUkK/view?usp=sharing
You can respond to a change event and do updates from any of the fields based on that. Here is an example: http://formio.github.io/formio.js/app/examples/externalload.html
@randallknutson i don't want add any custom code when render form like change event i want be more general . What i'm thinking it's when user create form can add "Custom Component" contains script can write script to calculated values and validation for all fields in one place.
it's doable or any suggest solution ?
You can do a single component that does custom validations in one place. However, we have a general rule that one component cannot directly modify another components value so you wouldn't be able to do calculated values for multiple fields in one place. The reason we do this is to keep the form clean and stop ambiguity like if two components were to attempt to modify the same component at the same time. You'll need to write the calculated values functionality in each component that is changed.
I think that's good to have.
We have a case where we need to call rest with JSON from different components values, we ended up replicating the code on each component on each change it got called multiple times. @randallknutson
Calculated value fires on each change of the form to ensure the values are correct. You may want to look at this example to see if it matches your use case: http://formio.github.io/formio.js/app/examples/externalload.html
Thanks @randallknutson for your replay.
Unfortunately, this approach will not work with us, because in our solution with have a single page service all our forms we just change the loaded form. so in our case, the solution has to be part of the form.
Currently, we have two have two issues:
-
We have a duplicate of code if there is a way I can define generic functions for example inside the HTML components. We have tried to add script tag inside the HTML components but that did not work it kept giving us function not defined.
-
Multiple REST calls, a huge number of calls.
Any ideas how we can handle this
This means you will need to create a custom renderer build that extends our core renderer, and then use your custom renderer build within your application. Here is a repo that illustrates how a custom renderer can be created.
https://github.com/formio/custom-renderer
Thanks @travist I will give it a try and update what happens with me