diff --git a/src/question.ts b/src/question.ts index f0d3b56bef..8f8a827958 100644 --- a/src/question.ts +++ b/src/question.ts @@ -1840,6 +1840,9 @@ export class Question extends SurveyElement this.survey.beforeSettingQuestionErrors(this, errors); } this.errors = errors; + if(this.errors !== errors) { + this.errors.forEach(er => er.locText.strChanged()); + } } this.updateContainsErrors(); if (oldHasErrors != errors.length > 0) { diff --git a/src/survey-error.ts b/src/survey-error.ts index 49b915d9e2..c0b661422d 100644 --- a/src/survey-error.ts +++ b/src/survey-error.ts @@ -44,7 +44,7 @@ export class SurveyError { return surveyLocalization.getString(locStrName, this.getLocale()); } public onUpdateErrorTextCallback: (error: SurveyError) => void = undefined; - public updateText() { + public updateText(): void { if(this.onUpdateErrorTextCallback) { this.onUpdateErrorTextCallback(this); } diff --git a/tests/surveyquestiontests.ts b/tests/surveyquestiontests.ts index a504c8cc3e..6607efce30 100644 --- a/tests/surveyquestiontests.ts +++ b/tests/surveyquestiontests.ts @@ -7090,4 +7090,45 @@ QUnit.test("Try to set incorrect values, bug#6629", function (assert) { assert.equal(q5.isEmpty(), true, "Can't set 'e' to multipletext"); assert.deepEqual(q6.value, ["f"], "Convert to array"); ConsoleWarnings.inCorrectQuestionValue = oldFunc; -}); \ No newline at end of file +}); +QUnit.test("Dynamic error text in expression validator, bug#6790", function (assert) { + const survey = new SurveyModel({ + checkErrorsMode: "onValueChanged", + elements: [ + { + type: "text", + name: "q1", + isRequired: true + }, + { + type: "expression", + name: "q2", + expression: "100 - {q1}", + validators: [ + { + type: "expression", + text: "{q2}% left.", + expression: "{leftover} <= 0" + } + ] + } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + q1.value = 10; + assert.equal(q2.errors.length, 1, "Error is here, #1"); + const error = q2.errors[0]; + assert.equal(q2.errors[0].locText.renderedHtml, "90% left.", "Error text is correct, #1"); + let errorTextChangedCounter = 0; + error.locText.onChanged = (): void => { + errorTextChangedCounter ++; + }; + q1.value = 20; + assert.equal(q2.errors.length, 1, "Error is here, #2"); + assert.equal(q2.errors[0].locText.renderedHtml, "80% left.", "Error text is correct, #2"); + assert.equal(error.locText.renderedHtml, "80% left.", "Old error text is correct, #2"); + assert.strictEqual(error, q2.errors[0], "Same errors"); + assert.equal(errorTextChangedCounter, 1, "text has been updated"); +}); +