Question T22451
Visible to All Users

expression calculated at loading time but does not change the survey data

created 12 days ago

Hi

in my scenario I have to load some survey with preloaded data. Some of this data are numbers represented as string:

Code
survey.data = { arg: 1, argPlusOne: '2', };

Even if all expressions are run at survey load the survey data does not change when the calculated value is different only as a type. To me 2 is different than "2" but for surveyjs 2 is "the same as "2". Test here

https://plnkr.co/edit/biZCxJPS3Og0HMqE?preview
The previous plunk just show that the expression is called. But even without the custom function the behavior is the same:

https://plnkr.co/edit/QiKR9qOehTLxGIQ0

I need to sanitize my data saved as string. I changed the survey to save numbers and I expect surveyjs to apply the new expression to the survey data AT LOADING TIME. Could you help please?

thank you

Show previous comments (1)

    Hi Jane, and sorry if I was not clear

    let's focus on https://plnkr.co/edit/biZCxJPS3Og0HMqE?preview.
    Basically, I would like that when the survey loads it would change the survey data from:

    { "arg": 1, "argPlusOne": "2" }

    to

    { "arg": 1, "argPlusOne": 2 }

    without any user interaction. Why? Because my custom function in the expression returns 2 and not "2".
    Instead if I click complete the result is still

    { "arg": 1, "argPlusOne": "2" }

    SurveyJS does not change the data because it maybe thinks that 2 and "2" mean the same. From a stricter data point of view, they are very different

    I hope is clearer now. Thank you

      Hello,
      Thank you for the update. You're correct. A form library uses the Helpers.isTwoValueEquals function to check if these two values (2 and "2") are equal. Specifically, those values are considered equal in the following line of code: https://github.com/surveyjs/survey-library/blob/7f1b3a0ec8bec46b8730cedecfcf673e436b0921/packages/survey-core/src/helpers.ts#L137-L141. Therefore, a survey doesn't assign a custom function calculation result 2 to the existing "argPlusOne": "2" value since these are considered equal.

      Would you please elaborate on your usage scenario? In particular, why are you assigning a value to your argPlusOne field if you want it to be calculated in code? You may wish to remove the argPlusOne value from the survey.data object instance:

      JavaScript
      survey.data = { arg: 1 };

      it's value will be calculated using a custom function.

      I look forward to your reply.

        Hi Jane and sorry for my late reply

        We use surveyJS as form builder to create and update json data. So our users repeatedly reopen the forms for edit. At the same time we rely heavily on the fact that the expressions are freshly run on each survey load. For some reasons (that I could explain but maybe is not so important) we expect that some expression results may change over time (even in their type) because behind the scenes the are based on results coming from web services that we don't control.

        We need to detect the type changes.

        Ideally, it would be great if you could provide a global setting to control the strictness of the equality function ``Helpers.isTwoValueEquals`

        thank you and good evening