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

surveyValidateQuestion not working for dynamic panel items #2039

Closed Robby123456 closed 3 years ago

Robby123456 commented 4 years ago

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

Reporting a bug

What is the current behavior?

Validation errors are not being displayed for questions inside dynamic panels

What is the expected behavior?

A validation message is displayed and survey should not complete

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

In the Plunker example:

  1. Add a new row in the Name List
  2. Press Complete

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

Tested page URL: https://plnkr.co/edit/Dh5xS496blwDDIp9

Test code

//assign call to onServerValidateQuestions callback
function surveyValidateQuestion(survey, options) {
    var name1 = options.data["panel1"][0].name1;
    var name2 = options.data["name2"];

    if (!name1) {
        options.errors["name1"] = "Please enter a value";   
        options.errors["panel.name1"] = "Please enter a value";   
        options.errors["panel1[0].name1"] = "Please enter a value";   
    }
    if (!name2) {
        options.errors["name2"] = "Please enter a value";   
    }
    options.complete();
}

Survey
    .StylesManager
    .applyTheme("modern");

var json = {
    questions: [
        {
        "type": "paneldynamic",
        "name": "panel1",
        "title": "Name List",
        "templateElements": [
        {
        "type": "text",
        "name": "name1"
        }
        ]
        },
        {
        "type": "text",
        "name": "name2"
        }
    ]
};

window.survey = new Survey.Model(json);

survey
    .onComplete
    .add(function (result) {
        document
            .querySelector('#surveyResult')
            .textContent = "Result JSON:\n" + JSON.stringify(result.data, null, 3);
    });

survey
    .onServerValidateQuestions
    .add(surveyValidateQuestion);
$("#surveyElement").Survey({model: survey});

Specify your

andrewtelnov commented 3 years ago

We missed this question. Here is the updated example.

Thank you, Andrew