Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The survey.onValueChanging event is not raised when updating a matrix… #8299

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5623,15 +5623,15 @@ export class SurveyModel extends SurveyElementCore
page.name = name;
return page;
}
protected questionOnValueChanging(valueName: string, newValue: any): any {
protected questionOnValueChanging(valueName: string, newValue: any, questionValueName?: string): any {
if (!!this.editingObj) {
const prop = Serializer.findProperty(this.editingObj.getType(), valueName);
if (!!prop) newValue = prop.settingValue(this.editingObj, newValue);
}
if (this.onValueChanging.isEmpty) return newValue;
var options = {
name: valueName,
question: <Question>this.getQuestionByValueName(valueName),
question: <Question>this.getQuestionByValueName(questionValueName || valueName),
value: this.getUnbindValue(newValue),
oldValue: this.getValue(valueName),
};
Expand Down Expand Up @@ -6600,7 +6600,8 @@ export class SurveyModel extends SurveyElementCore
public setComment(name: string, newValue: string, locNotification: any = false): void {
if (!newValue) newValue = "";
if (this.isTwoValueEquals(newValue, this.getComment(name))) return;
var commentName = name + this.commentSuffix;
const commentName = name + this.commentSuffix;
newValue = this.questionOnValueChanging(commentName, newValue, name);
if (this.isValueEmpty(newValue)) {
this.deleteDataValueCore(this.valuesHash, commentName);
} else {
Expand All @@ -6626,6 +6627,10 @@ export class SurveyModel extends SurveyElementCore
question: question,
value: newValue,
});
question.comment = newValue;
if(question.comment != newValue) {
question.comment = newValue;
}
}
}
/**
Expand Down
65 changes: 64 additions & 1 deletion tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,70 @@ QUnit.test("onValueChanging event", function (assert) {
"onValueChanging event allows to change value"
);
});
QUnit.test("onValueChanging event - do not allow clear value, #1542", function (
QUnit.test("onValueChanging event for comment, #8292", function (
assert
) {
const survey = new SurveyModel();
const page = survey.addNewPage("page");
const q1 = page.addNewQuestion("text", "q1");
q1.showCommentArea = true;
let oldComment, newComment, questionName;
survey.onValueChanging.add((sender, options) => {
if (options.name == "q1-Comment") {
questionName = options.question.name;
oldComment = options.oldValue;
newComment = options.value;
}
});
q1.comment = "abc";
assert.equal(questionName, "q1", "question name #1");
assert.equal(q1.comment, "abc", "comment value #1");
assert.equal(newComment, "abc", "newComment #1");
q1.comment = "abcd";
assert.equal(q1.comment, "abcd", "comment value #2");
assert.equal(newComment, "abcd", "newComment #2");
assert.equal(oldComment, "abc", "newComment #2");
});

QUnit.test("onValueChanging event - do not allow clear value, #8292", function (
assert
) {
const survey = new SurveyModel();
const page = survey.addNewPage("page");
const q1 = page.addNewQuestion("text", "q1");
q1.showCommentArea = true;
let counter = 0;
let oldComment, newComment, questionName;
survey.onValueChanging.add((sender, options) => {
if (options.name === "q1-Comment") {
counter ++;
questionName = options.question.name;
if(!options.value) {
options.value = options.oldValue;
}
oldComment = options.oldValue;
newComment = options.value;
}
});
let onValueChangedValue;
survey.onValueChanged.add((sender, options) => {
if (options.name === "q1-Comment") {
onValueChangedValue = options.value;
}
});
q1.comment = "abc";
assert.equal(counter, 1, "counter #1");
assert.equal(q1.comment, "abc", "comment value #1");
q1.comment = "";
assert.equal(questionName, "q1", "question.name #2");
assert.equal(oldComment, "abc", "oldComment #2");
assert.equal(newComment, "abc", "newComment #2");
assert.equal(counter, 2, "counter #2");
assert.equal(q1.comment, "abc", "comment value #2");
assert.equal(survey.getComment("q1"), "abc", "survey.getComment value #2");
assert.equal(onValueChangedValue, "abc", "onValueChanged options.value");
});
QUnit.test("onValueChanging event - do not allow clear value in comment, #1542", function (
assert
) {
var survey = new SurveyModel();
Expand Down
Loading