Skip to content

Commit

Permalink
Bug/6790 validator incorrect text (#6808)
Browse files Browse the repository at this point in the history
* Add unit test #6790

* Validator text containing an expression doesn't update properly on value change fix #6790
  • Loading branch information
andrewtelnov committed Aug 26, 2023
1 parent 80f051d commit 1663bef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,9 @@ export class Question extends SurveyElement<Question>
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/survey-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
43 changes: 42 additions & 1 deletion tests/surveyquestiontests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
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");
});

0 comments on commit 1663bef

Please sign in to comment.