From 9bef72f168f408b0029dc4ee0b10565e7c75527b Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Tue, 14 Jan 2025 15:33:40 +0200 Subject: [PATCH] refactor onValidatePanel event, add errors --- packages/survey-core/src/base-interfaces.ts | 3 +- packages/survey-core/src/panel.ts | 10 ++----- packages/survey-core/src/survey-events-api.ts | 4 +++ packages/survey-core/src/survey.ts | 28 +++++++++-------- packages/survey-core/tests/surveytests.ts | 30 +++++++++++++++++++ 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/packages/survey-core/src/base-interfaces.ts b/packages/survey-core/src/base-interfaces.ts index 220277bc22..f51a6f5752 100644 --- a/packages/survey-core/src/base-interfaces.ts +++ b/packages/survey-core/src/base-interfaces.ts @@ -92,7 +92,7 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner { ): any; focusQuestionByInstance(question: IQuestion, onError: boolean): boolean; validateQuestion(question: IQuestion, errors: Array, fireCallback: boolean): void; - validatePanel(panel: IPanel): SurveyError; + validatePanel(panel: IPanel, errors: Array, fireCallback: boolean): void; hasVisibleQuestionByValueName(valueName: string): boolean; questionsByValueName(valueName: string): Array; processHtml(html: string, reason: string): string; @@ -125,7 +125,6 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner { rootElement?: HTMLElement; requiredMark: string; - beforeSettingPanelErrors(question: IPanel, errors: Array): void; getQuestionDisplayValue(question: IElement, displayValue: any): any; getSurveyErrorCustomText(obj: Base, text: string, error: SurveyError): string; getElementTitleTagName(element: Base, tagName: string): string; diff --git a/packages/survey-core/src/panel.ts b/packages/survey-core/src/panel.ts index 54735f2f7e..1a6bba382d 100644 --- a/packages/survey-core/src/panel.ts +++ b/packages/survey-core/src/panel.ts @@ -954,17 +954,13 @@ export class PanelModelBase extends SurveyElement private hasErrorsInPanels(rec: any): void { var errors = >[]; this.hasRequiredError(rec, errors); - if (this.isPanel && this.survey) { - var customError = this.survey.validatePanel(this); - if (customError) { - errors.push(customError); + if (this.survey) { + this.survey.validatePanel(this, errors, rec.fireCallback); + if(errors.length > 0) { rec.result = true; } } if (!!rec.fireCallback) { - if (!!this.survey) { - this.survey.beforeSettingPanelErrors(this, errors); - } this.errors = errors; } } diff --git a/packages/survey-core/src/survey-events-api.ts b/packages/survey-core/src/survey-events-api.ts index c98aa68a19..cf138c177f 100644 --- a/packages/survey-core/src/survey-events-api.ts +++ b/packages/survey-core/src/survey-events-api.ts @@ -345,6 +345,10 @@ export interface ValidatePanelEvent extends PanelEventMixin { * The panel's name. */ name: string; + /** + * An array of validation errors. + */ + errors: Array; } export interface ErrorCustomTextEvent { /** diff --git a/packages/survey-core/src/survey.ts b/packages/survey-core/src/survey.ts index 7c7601c578..e4e6c6d917 100644 --- a/packages/survey-core/src/survey.ts +++ b/packages/survey-core/src/survey.ts @@ -1795,9 +1795,6 @@ export class SurveyModel extends SurveyElementCore errors: errors, }); } - beforeSettingPanelErrors(question: IPanel, errors: Array): void { - this.makeRequiredErrorsInvisible(errors); - } private makeRequiredErrorsInvisible(errors: Array) { if (!this.hideRequiredErrors) return; for (var i = 0; i < errors.length; i++) { @@ -7360,15 +7357,22 @@ export class SurveyModel extends SurveyElementCore this.beforeSettingQuestionErrors(question, errors); } } - validatePanel(panel: PanelModel): SurveyError { - if (this.onValidatePanel.isEmpty) return null; - var options = { - name: panel.name, - panel: panel, - error: null, - }; - this.onValidatePanel.fire(this, options); - return options.error ? new CustomError(options.error, this) : null; + validatePanel(panel: PanelModel, errors: Array, fireCallback: boolean): void { + if (panel.isPanel && !this.onValidatePanel.isEmpty) { + const options = { + name: panel.name, + panel: panel, + error: null, + errors: errors + }; + this.onValidatePanel.fire(this, options); + if(options.error) { + errors.push(new CustomError(options.error, this)); + } + } + if(fireCallback) { + this.makeRequiredErrorsInvisible(errors); + } } processHtml(html: string, reason?: string): string { if (!reason) reason = ""; diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 0a665bd028..bb30cd1912 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -69,6 +69,7 @@ import { ListModel } from "../src/list"; import { _setIsTouch } from "../src/utils/devices"; import { oldDefaultTheme, setOldTheme } from "./oldTheme"; import { ConsoleWarnings } from "../src/console-warnings"; +import { CustomError } from "../src/error"; export default QUnit.module("Survey"); @@ -2595,6 +2596,35 @@ QUnit.test("onValidatePanel test", function (assert) { ); assert.equal(counter, 3, "onValidatePanel calls three time"); }); +QUnit.test("survey.onValidatePanel & options.errors", function (assert) { + const survey = new SurveyModel(); + const page = survey.addNewPage("page"); + const panel = page.addNewPanel("panel"); + const q1 = panel.addNewQuestion("text", "q1"); + const q2 = panel.addNewQuestion("text", "q2"); + survey.onValidatePanel.add(function (sender, options) { + const panel = options.panel; + const pq1 = panel.getQuestionByName("q1"); + const pq2 = panel.getQuestionByName("q2"); + const sum = pq1.value + pq1.value; + if (sum < 10 || pq1.isEmpty() || pq2.isEmpty()) { + options.errors.push(new CustomError("q1+q2 should be more than 9")); + } + if (sum >= 100) { + options.errors.push(new CustomError("q1+q2 should be less than 100")); + } + }); + + assert.equal(survey.isCurrentPageHasErrors, true, "failed, values are undefined : 10 < q1.value + q2.value < 100"); + assert.equal(panel.errors.length, 1, "panel.errors, #1"); + q1.value = 5; + q2.value = 50; + assert.equal(survey.isCurrentPageHasErrors, false, "passed: 5 + 50, 10 < q1.value + q2.value < 100"); + assert.equal(panel.errors.length, 0, "panel.errors, #2"); + q1.value = 55; + assert.equal(survey.isCurrentPageHasErrors, true, "failed: 55 + 50, 10 < q1.value + q2.value < 100"); + assert.equal(panel.errors.length, 1, "panel.errors, #3"); +}); QUnit.test( "isCurrentPageHasErrors, required question in the invisible panel, #325", function (assert) {