diff --git a/src/question_baseselect.ts b/src/question_baseselect.ts index 9f157d43c8..a8275b4e1f 100644 --- a/src/question_baseselect.ts +++ b/src/question_baseselect.ts @@ -1601,24 +1601,20 @@ export class QuestionSelectBase extends Question { private get hasChoicesUrl(): boolean { return this.choicesByUrl && !!this.choicesByUrl.url; } - public clearIncorrectValues() { - if (!this.hasValueToClearIncorrectValues()) return; - if(this.carryForwardQuestion && !this.carryForwardQuestion.isReady) return; - if ( - !!this.survey && - this.survey.questionsByValueName(this.getValueName()).length > 1 - ) - return; - if (this.hasChoicesUrl && - (!this.choicesFromUrl || this.choicesFromUrl.length == 0) - ) - return; + public clearIncorrectValues(): void { + if (!this.hasValueToClearIncorrectValues() || !this.canClearIncorrectValues()) return; if (this.clearIncorrectValuesCallback) { this.clearIncorrectValuesCallback(); } else { this.clearIncorrectValuesCore(); } } + private canClearIncorrectValues(): boolean { + if(this.carryForwardQuestion && !this.carryForwardQuestion.isReady) return false; + if (!!this.survey && this.survey.questionsByValueName(this.getValueName()).length > 1) return false; + if (this.hasChoicesUrl && (!this.choicesFromUrl || this.choicesFromUrl.length == 0)) return false; + return true; + } protected hasValueToClearIncorrectValues(): boolean { if(!!this.survey && this.survey.keepIncorrectValues) return false; return !this.keepIncorrectValues && !this.isEmpty(); diff --git a/src/question_paneldynamic.ts b/src/question_paneldynamic.ts index a7b92d2816..310d94ba0f 100644 --- a/src/question_paneldynamic.ts +++ b/src/question_paneldynamic.ts @@ -1702,10 +1702,12 @@ export class QuestionPanelDynamicModel extends Question } private clearValueInPanelsIfInvisible(reason: string): void { for (var i = 0; i < this.panelsCore.length; i++) { - var questions = this.panelsCore[i].questions; + const panel = this.panelsCore[i]; + var questions = panel.questions; this.isSetPanelItemData = {}; for (var j = 0; j < questions.length; j++) { const q = questions[j]; + if(q.visible && !panel.isVisible) continue; q.clearValueIfInvisible(reason); this.isSetPanelItemData[q.getValueName()] = this.maxCheckCount + 1; } diff --git a/tests/question_paneldynamic_tests.ts b/tests/question_paneldynamic_tests.ts index e6e0210997..8c4a518cb1 100644 --- a/tests/question_paneldynamic_tests.ts +++ b/tests/question_paneldynamic_tests.ts @@ -6677,3 +6677,141 @@ QUnit.test("panel dynamic & addPanel/removePanel with non-build panels, #7693", assert.equal(dynamicPanel.panelCount, 2, "panelCount #3"); assert.equal(dynamicPanel.getQuestionFromArray("q2", 0).name, "q2", "Panels are created"); }); +QUnit.test("panel dynamic & panel visibleIf & checkbox vs carry forward, #7693", function (assert) { + const survey = new SurveyModel({ + "pages": [ + { + "elements": [ + { + "type": "paneldynamic", + "name": "aboutMe", + "valueName": "household", + "templateElements": [ + { + "type": "text", + "name": "firstName" + }, + { + "type": "text", + "name": "lastName" + }, + { + "type": "expression", + "name": "fullName", + "visible": false, + "expression": "{panel.firstName} + ' ' + {panel.lastName}" + } + ], + "panelCount": 2, + "minPanelCount": 1, + "templateVisibleIf": "{panelIndex} = 0" + } + ] + }, + { + "elements": [ + { + "type": "paneldynamic", + "name": "addChildren", + "valueName": "household", + "isRequired": true, + "templateElements": [ + { + "type": "text", + "name": "childFirstName", + "valueName": "firstName" + }, + { + "type": "text", + "name": "childLastName", + "valueName": "lastName" + }, + { + "type": "expression", + "name": "childFullName", + "visible": false, + "valueName": "fullName", + "expression": "{panel.firstName} + ' ' + {panel.lastName}" + } + ], + "panelCount": 1, + "minPanelCount": 1, + "templateVisibleIf": "{panelIndex} > 0" + } + ] + }, + { + "elements": [ + { + "type": "checkbox", + "name": "addIncomeFor", + "choicesFromQuestion": "addChildren", + "choiceValuesFromQuestion": "fullName" + } + ] + }, + { + "elements": [ + { + "type": "paneldynamic", + "name": "householdIncome", + "valueName": "household", + "templateElements": [ + { + "type": "text", + "name": "question1" + } + ], + "templateTitle": "{panel.fullName}", + "panelsState": "collapsed", + "templateVisibleIf": "{addIncomeFor} contains {panel.fullName}" + } + ] + } + ] + }); + survey.data = { + "household": [ + { + "firstName": "first1", + "fullName": "first1 last1", + "lastName": "last1", + "question1": "text1" + }, + { + "firstName": "first2", + "fullName": "first2 last2", + "lastName": "last2" + }, + { + "firstName": "first3", + "fullName": "first3 last3", + "lastName": "last3", + "question1": "test3" + } + ], + "addIncomeFor": [ + "first1 last1", + "first3 last3" + ] + }; + survey.doComplete(); + assert.deepEqual(survey.data, { + "household": [ + { + "firstName": "first1", + "lastName": "last1", + "question1": "text1" + }, + { + "firstName": "first2", + "lastName": "last2" + }, + { + "firstName": "first3", + "lastName": "last3", + "question1": "test3" + } + ] + }); +});