From 37a1b4d326d520083415fea7f6c7b57087b178f3 Mon Sep 17 00:00:00 2001 From: Andrew Telnov Date: Mon, 11 Sep 2023 14:13:41 +0300 Subject: [PATCH] Checkboxes Question: Choices selected are not reflected in another field using Default Value Expression Property fix #6886 --- src/question.ts | 10 ++++------ src/question_comment.ts | 1 + src/question_expression.ts | 4 ++++ src/question_textbase.ts | 5 +++++ tests/surveyquestiontests.ts | 18 ++++++++++++++++++ 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/question.ts b/src/question.ts index 1949b2e6d2..a10f3f0d50 100644 --- a/src/question.ts +++ b/src/question.ts @@ -2050,12 +2050,9 @@ export class Question extends SurveyElement protected canSetValueToSurvey(): boolean { return true; } - protected valueFromData(val: any): any { - return val; - } - protected valueToData(val: any): any { - return val; - } + protected valueFromData(val: any): any { return val; } + protected valueToData(val: any): any { return val; } + protected convertToCorrectValue(val: any): any { return val; } protected onValueChanged(): void { } protected isMouseDown: boolean; onMouseDown(): void { @@ -2096,6 +2093,7 @@ export class Question extends SurveyElement this.isValueChangedDirectly = true; } protected setQuestionValue(newValue: any, updateIsAnswered: boolean = true): void { + newValue = this.convertToCorrectValue(newValue); const isEqual = this.isTwoValueEquals(this.questionValue, newValue); if (!isEqual && !this.isChangingViaDefaultValue) { this.setValueChangedDirectly(); diff --git a/src/question_comment.ts b/src/question_comment.ts index 08c05fc2e9..453678f58e 100644 --- a/src/question_comment.ts +++ b/src/question_comment.ts @@ -111,6 +111,7 @@ export class QuestionCommentModel extends QuestionTextBase { } super.setNewValue(newValue); } + protected getValueSeparator(): string { return "\n"; } public get className() { return (this.cssClasses ? this.getControlClass() : "panel-comment-root") || undefined; } diff --git a/src/question_expression.ts b/src/question_expression.ts index 515312075d..1d982ce145 100644 --- a/src/question_expression.ts +++ b/src/question_expression.ts @@ -236,6 +236,10 @@ export class QuestionExpressionModel extends Question { } return val.toString(); } + protected convertToCorrectValue(val: any): any { + if(Array.isArray(val)) return val.join(", "); + return val; + } } export function getCurrecyCodes(): Array { diff --git a/src/question_textbase.ts b/src/question_textbase.ts index e8958ac004..fb1b0f8d73 100644 --- a/src/question_textbase.ts +++ b/src/question_textbase.ts @@ -120,6 +120,11 @@ export class QuestionTextBase extends Question { super.setQuestionValue(newValue, updateIsAnswered); this.updateRemainingCharacterCounter(newValue); } + protected convertToCorrectValue(val: any): any { + if(Array.isArray(val)) return val.join(this.getValueSeparator()); + return val; + } + protected getValueSeparator(): string { return ", "; } public disableNativeUndoRedo = false; protected checkForUndo(event: KeyboardEvent) { if (this.disableNativeUndoRedo && this.isInputTextUpdate && (event.ctrlKey || event.metaKey)) { diff --git a/tests/surveyquestiontests.ts b/tests/surveyquestiontests.ts index 1760cd5ad9..89c94eecb5 100644 --- a/tests/surveyquestiontests.ts +++ b/tests/surveyquestiontests.ts @@ -7156,4 +7156,22 @@ QUnit.test("Dynamic error text in expression validator, bug#6790", function (ass 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"); +}); +QUnit.test("Set array and convert it to a string, bug#6886", function (assert) { + const survey = new SurveyModel({ + elements: [ + { type: "text", name: "q1" }, + { type: "comment", name: "q2" }, + { type: "expression", name: "q3" } + ] + }); + const q1 = survey.getQuestionByName("q1"); + const q2 = survey.getQuestionByName("q2"); + const q3 = survey.getQuestionByName("q3"); + q1.value = ["item1", "item2", "item3"]; + q2.value = ["item1", "item2", "item3"]; + q3.value = ["item1", "item2", "item3"]; + assert.equal(q1.value, "item1, item2, item3", "q1"); + assert.equal(q2.value, "item1\nitem2\nitem3", "q2"); + assert.equal(q3.value, "item1, item2, item3", "q3"); }); \ No newline at end of file