Skip to content

Commit

Permalink
On selecting otherItem the error is shown if errorMode is onValueChan…
Browse files Browse the repository at this point in the history
…ged fix #8369 (#8370)
  • Loading branch information
andrewtelnov authored Jun 5, 2024
1 parent afc758d commit ec519e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,19 @@ export class QuestionSelectBase extends Question {
ItemValue.locStrsChanged(this.visibleChoices);
}
}
private prevOtherErrorValue: string;
private updatePrevOtherErrorValue(val: string): void {
const oldVal = this.otherValue;
if(val !== oldVal) {
this.prevOtherErrorValue = oldVal;
}
}
public get otherValue(): string {
if(!this.showCommentArea) return this.comment;
return this.otherValueCore;
}
public set otherValue(val: string) {
this.updatePrevOtherErrorValue(val);
if(!this.showCommentArea) {
this.comment = val;
} else {
Expand Down Expand Up @@ -545,6 +553,7 @@ export class QuestionSelectBase extends Question {
}
private isSettingComment: boolean = false;
protected setQuestionComment(newValue: string): void {
this.updatePrevOtherErrorValue(newValue);
if(this.showCommentArea) {
super.setQuestionComment(newValue);
return;
Expand Down Expand Up @@ -1342,7 +1351,8 @@ export class QuestionSelectBase extends Question {
isOnValueChanged: boolean
) {
super.onCheckForErrors(errors, isOnValueChanged);
if (!this.hasOther || !this.isOtherSelected || this.otherValue) return;
if (!this.hasOther || !this.isOtherSelected || this.otherValue
|| isOnValueChanged && !this.prevOtherErrorValue) return;
const otherEmptyError = new OtherEmptyError(this.otherErrorText, this);
otherEmptyError.onUpdateErrorTextCallback = err => { err.text = this.otherErrorText; };
errors.push(otherEmptyError);
Expand Down
11 changes: 11 additions & 0 deletions tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,17 @@ QUnit.test("selectbase and otherValue/comment + same values", (assert) => {
assert.deepEqual(["item33"], q2.value, "q2 value, #5");
assert.equal("item33", q2.otherValue, "q2 otherValue, #6");
});
QUnit.test("selectbase, showOtherItem & checkErrorsMode: 'onValueChanged'", (assert) => {
const survey = new SurveyModel({ elements: [
{ type: "radiogroup", name: "q1", showOtherItem: true, choices: ["item1", "item2", "item3"] },
], checkErrorsMode: "onValueChanged" });
const q1 = <QuestionSelectBase>survey.getQuestionByName("q1");
q1.renderedValue = "other";
assert.equal(q1.errors.length, 0, "We do not have errors yet");
survey.completeLastPage();
assert.equal(q1.errors.length, 1, "There is an error");
assert.equal(survey.state, "running", "Still running");
});
QUnit.test("selectbase, otherValue&question-Comment", (assert) => {
const survey = new SurveyModel({ elements: [
{ type: "dropdown", name: "q1", showOtherItem: true, choices: ["item1", "item2", "item3"] },
Expand Down
12 changes: 10 additions & 2 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13043,9 +13043,17 @@ QUnit.test(
});
var q1 = <QuestionDropdownModel>survey.getQuestionByName("q1");
q1.value = q1.otherItem.value;
assert.equal(q1.errors.length, 0, "There is no error yet");
q1.comment = "some value1";
assert.equal(q1.errors.length, 0, "There is no error - there is a value");
q1.comment = "";
assert.equal(q1.errors.length, 1, "There is an error right now");
q1.comment = "some value";
assert.equal(q1.errors.length, 0, "There is no error now");
q1.comment = "some value2";
assert.equal(q1.errors.length, 0, "There is no error again");
q1.value = 1;
q1.value = q1.otherItem.value;
assert.equal(q1.comment, "", "Comment is empty");
assert.equal(q1.errors.length, 0, "There is no error - comment was cleaned");
}
);
QUnit.test(
Expand Down

0 comments on commit ec519e6

Please sign in to comment.