Whenever your validation looks like it is a step behind, you very likely have a problem with your calculation order. Something gets calculated before the necessary information is available. Look at your calculation order and make sure that things that other calculations depend on are calculated first.
You can't just plug a calculation script as a validation script... They are different things that access different properties of the event object, for different purposes.
As noted the most likely suspect is calculated fields being calculated in the incorrect order. This can be adjusted by reordering the fields to be calculated in the correct order or writing one calculation script for the last field to be calculated that performs all the needed calculations and sets the values of the calculated fields.
Note the calculation order for fields is different from the tab order for field entry and the reading order for a PDF.
Thanks for the answers so far.
So, let me take this back from the start. Is there a way to make it so the Form doesn't keep recalculating every calculation all the time? Calculate only the stuff that is affect by a field that gets a new value? That is what I'm trying to do.
Otherwise just setting calling my script as Calculation for each Total field works fine. I was trying to figure out if there is a way to change, say, [B.20], and having it recalculate only [Total.20], instead of going through all the Totals.
Unless my understanding is wrong and it does recalculate only what is relevant by default?
No, you cannot do selective calculations. When the state of your form changes, all fields with calculation scripts will be re-calculated.
The only way you can do this differently is by having only one calculation script, implemented as a document level script. Then you call this script from one field. In this script, you can then determine which field was changed (using the event.source property), and the tailor your calculation to that. I don't bother with that, I just use one calculation script to get around any problems with the calculation order.
Assume the names of four text fields: “total.0”, “A.0”, “B.0”, and “C.0”. The value of “total.0” is the sum total of the values of
“A.0”, “B.0” and “C.0”.
Assume each of “A.0”, “B.0” and “C.0” has a validation script.
Assume the following script defined at the document level:
var t0 = this.getField(“total.0”);
var a0 = this.getField(“A.0”);
var b0 = this.getField(“B.0”);
var c0 = this.getField(“C.0”);
Assume the user has entered 5 in field “A.0”.
When the validation script runs for “A.0” and if the validation script uses event.value and a0.value, then the script will
find that event.value is 5 while a0.value is still 3.
Another way to compute the value of “total.0” when the value of “A.0” changes would be to perform the following computation in the
validation script of “A.0”:
t0.value = t0.value + (event.value – a0.value);
A similar computation is performed in the validation script of “B.0”:
t0.value = t0.value + (event.value – b0.value);
And a similar computation is performed in the validation script of “C.0”:
t0.value = t0.value + (event.value – c0.value);