surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.01k stars 782 forks source link

clearInvisibleValues includes prefilled values from not answered questions #8304

Closed kasar-NTT closed 1 month ago

kasar-NTT commented 1 month ago

Are you requesting a feature, reporting a bug or asking a question?

Bug

What is the current behavior?

Hidden responded values (answers from next pages) are included onComplete

What is the expected behavior?

Only answered questions should be included onComplete

How would you reproduce the current behavior (if this is a bug)?

You can see the data from next pages on console of sandbox

image

Provide the test code and the tested page URL (if applicable)

Tested page URL: https://codesandbox.io/p/sandbox/surveyjs-angular-forked-llsrhs?file=%2Fsrc%2Fapp%2Fcomponents%2Fsurvey.component.ts

Test code

**json.ts**
export const json = {
  clearInvisibleValues: "onHidden",
  triggers: [
    {
      type: "complete",
      expression: "{exit1-test} = 'Yes'",
    },
    {
      type: "complete",
      expression: "{exit2} = 'Yes'",
    },
  ],
  pages: [
    {
      title: "What operating system do you use?",
      elements: [
        {
          type: "checkbox",
          name: "opSystem",
          title: "OS",
          showOtherItem: true,
          choices: ["Windows", "Linux", "Macintosh OSX"],
        },
        {
          type: "radiogroup",
          name: "exit1-test",
          title: "Do you want to finish the survey?",
          choices: ["Yes", "No"],
          colCount: 0,
        },
      ],
    },
    {
      title: "What language(s) are you currently using?",
      elements: [
        {
          type: "checkbox",
          name: "langs",
          title: "Please select from the list",
          colCount: 4,
          choices: [
            "Javascript",
            "Java",
            "Python",
            "CSS",
            "PHP",
            "Ruby",
            "C++",
            "C",
            "Shell",
            "C#",
            "Objective-C",
            "R",
            "VimL",
            "Go",
            "Perl",
            "CoffeeScript",
            "TeX",
            "Swift",
            "Scala",
            "Emacs Lisp",
            "Haskell",
            "Lua",
            "Clojure",
            "Matlab",
            "Arduino",
            "Makefile",
            "Groovy",
            "Puppet",
            "Rust",
            "PowerShell",
          ],
        },
        {
          type: "radiogroup",
          name: "exit2",
          title: "Do you want to finish the survey?",
          choices: ["Yes", "No"],
          colCount: 0,
        },
      ],
    },
    {
      title: "Please enter your name and e-mail",
      elements: [
        {
          type: "text",
          name: "name",
          title: "Name:",
        },
        {
          type: "text",
          name: "email",
          title: "Your e-mail",
        },
      ],
    },
  ],
};

**survey.component.ts**

import { Component, OnInit } from "@angular/core";
import { Model } from "survey-core";
import { json } from "./json";

@Component({
  // tslint:disable-next-line:component-selector
  selector: "component-survey",
  templateUrl: "./survey.component.html",
  styleUrls: ["./survey.component.css"],
})
export class SurveyComponent implements OnInit {
  model: Model;
  ngOnInit() {
    const survey = new Model(json);
    survey.mergeData({
      opSystem: "Windows",
      "exit1-test": "No",
      langs: "Javascript",
      name: "TestName",
      email: "test@email.com",
    });

    survey.onComplete.add((sender, options) => {
      console.log(JSON.stringify(sender.data, null, 3));
    });
    this.model = survey;
  }
}

Specify your

andrewtelnov commented 1 month ago

@kasar-NTT You have set the data using "mergeData". Why should SurveyJS remove them? Using what kind of logic? You can write your own logic by using onValueChanged event and mark questions that has been changed during your session. Based on this information, you can include into your results questions that has been modified during the last session.

Thank you, Andrew

kasar-NTT commented 1 month ago

@andrewtelnov i thought the property "clearInvisblevalues": "onComplete" , would handle that scenario, but it seems that it doesnt. The docs mention: Clears invisible question values when the survey is complete

andrewtelnov commented 1 month ago

@kasar-NTT If you make questions invisible then the values will not be in survey.data on "onComplete" the default behavior. It is about values in invisible questions. I am talking about visible/visibleIf question/page/panel properties. Your questions on the last page are visible. Any other behavior would be treated as a bug by the most of the developers here.

Thank you, Andrew

kasar-NTT commented 1 month ago

@andrewtelnov so in order to achieve "my purpose", the only way to do it, would be to have a condition on the questions of the last page that checks the value of the answer that "completes" the questionnaire. Am i right? I' ll try that approach and come back to you

Best regards, Kostas

andrewtelnov commented 1 month ago

@kasar-NTT I wrote you early. You can create your own hash, set it on survey.onValueChanged and send to the server your custom hash of changed values.

Thank you, Andrew

andrewtelnov commented 1 month ago
const changedValues = {};
survey.onValueChanged.add((sender, options) => {
  changedValues[options.name] = options.value;
});

On complete you can send to your server changedValues object.

Thank you, Andrew