From b196f8b4580293ea30c3e39e0ed6ccf7e26c460e Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Fri, 3 May 2024 14:23:00 +0300 Subject: [PATCH] The focusOnFirstError: false parameter within the survey.validate() function doesn't prevent an invalid value from being focused fix #8228 --- src/panel.ts | 2 +- tests/question_paneldynamic_tests.ts | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/panel.ts b/src/panel.ts index 48371e3fe1..1b81761ce3 100644 --- a/src/panel.ts +++ b/src/panel.ts @@ -857,7 +857,7 @@ export class PanelModelBase extends SurveyElement }; if(rec.result !== true) rec.result = false; this.hasErrorsCore(rec); - if (rec.firstErrorQuestion) { + if (rec.focuseOnFirstError && rec.firstErrorQuestion) { rec.firstErrorQuestion.focus(true); } return !rec.result; diff --git a/tests/question_paneldynamic_tests.ts b/tests/question_paneldynamic_tests.ts index 55dd186317..2ff29067cd 100644 --- a/tests/question_paneldynamic_tests.ts +++ b/tests/question_paneldynamic_tests.ts @@ -19,6 +19,7 @@ import { settings } from "../src/settings"; import { QuestionMatrixModel } from "../src/question_matrix"; import { defaultStandardCss } from "../src/defaultCss/cssstandard"; import { AnimationGroup, AnimationTab } from "../src/utils/animation"; +import { SurveyElement } from "../src/survey-element"; export default QUnit.module("Survey_QuestionPanelDynamic"); @@ -7148,3 +7149,34 @@ QUnit.test("onQuestionVisibleChanged should be fired", function (assert) { survey.setValue("q1", 2); assert.deepEqual(questionNames, ["q3:true", "q3:false"], "visiblity logs"); }); +QUnit.test("Always focus on error in duplicated value, Bug8228", function (assert) { + let focusedQuestionId = ""; + var oldFunc = SurveyElement.FocusElement; + SurveyElement.FocusElement = function (elId: string): boolean { + focusedQuestionId = elId; + return true; + }; + const survey = new SurveyModel({ + "elements": [{ + "name": "q1", + "type": "paneldynamic", + "keyName": "q2", + "templateElements": [ + { + "name": "q2", + "type": "text" + } + ] + }, + ] + }); + survey.data = { q1: [{ q2: 2 }, { q2: 2 }] }; + + const res = survey.validate(false, false); + assert.equal(res, false, "There is an error"); + assert.notOk(focusedQuestionId, "Do not focus"); + survey.validate(false, true); + assert.ok(focusedQuestionId, "Focus on the question"); + + SurveyElement.FocusElement = oldFunc; +}); \ No newline at end of file