Skip to content

Commit

Permalink
refactor onValidatePanel event, add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jan 14, 2025
1 parent 00ffe62 commit 9bef72f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
3 changes: 1 addition & 2 deletions packages/survey-core/src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
): any;
focusQuestionByInstance(question: IQuestion, onError: boolean): boolean;
validateQuestion(question: IQuestion, errors: Array<SurveyError>, fireCallback: boolean): void;
validatePanel(panel: IPanel): SurveyError;
validatePanel(panel: IPanel, errors: Array<SurveyError>, fireCallback: boolean): void;
hasVisibleQuestionByValueName(valueName: string): boolean;
questionsByValueName(valueName: string): Array<IQuestion>;
processHtml(html: string, reason: string): string;
Expand Down Expand Up @@ -125,7 +125,6 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
rootElement?: HTMLElement;

requiredMark: string;
beforeSettingPanelErrors(question: IPanel, errors: Array<SurveyError>): void;
getQuestionDisplayValue(question: IElement, displayValue: any): any;
getSurveyErrorCustomText(obj: Base, text: string, error: SurveyError): string;
getElementTitleTagName(element: Base, tagName: string): string;
Expand Down
10 changes: 3 additions & 7 deletions packages/survey-core/src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,17 +954,13 @@ export class PanelModelBase extends SurveyElement<Question>
private hasErrorsInPanels(rec: any): void {
var errors = <Array<any>>[];
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;
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/survey-core/src/survey-events-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ export interface ValidatePanelEvent extends PanelEventMixin {
* The panel's name.
*/
name: string;
/**
* An array of validation errors.
*/
errors: Array<SurveyError>;
}
export interface ErrorCustomTextEvent {
/**
Expand Down
28 changes: 16 additions & 12 deletions packages/survey-core/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1795,9 +1795,6 @@ export class SurveyModel extends SurveyElementCore
errors: errors,
});
}
beforeSettingPanelErrors(question: IPanel, errors: Array<SurveyError>): void {
this.makeRequiredErrorsInvisible(errors);
}
private makeRequiredErrorsInvisible(errors: Array<SurveyError>) {
if (!this.hideRequiredErrors) return;
for (var i = 0; i < errors.length; i++) {
Expand Down Expand Up @@ -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: <any>null,
};
this.onValidatePanel.fire(this, options);
return options.error ? new CustomError(options.error, this) : null;
validatePanel(panel: PanelModel, errors: Array<SurveyError>, fireCallback: boolean): void {
if (panel.isPanel && !this.onValidatePanel.isEmpty) {
const options = {
name: panel.name,
panel: panel,
error: <any>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 = "";
Expand Down
30 changes: 30 additions & 0 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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 = <QuestionTextModel>panel.addNewQuestion("text", "q1");
const q2 = <QuestionTextModel>panel.addNewQuestion("text", "q2");
survey.onValidatePanel.add(function (sender, options) {
const panel = <PanelModel>options.panel;
const pq1 = <QuestionTextModel>panel.getQuestionByName("q1");
const pq2 = <QuestionTextModel>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) {
Expand Down

0 comments on commit 9bef72f

Please sign in to comment.