Skip to content

Commit

Permalink
Choices are updated incorrectly after fixing the value duplication er…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 27, 2024
1 parent 56f0eec commit 4aaa59a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 5 additions & 4 deletions packages/survey-core/src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,11 +2227,12 @@ export class Question extends SurveyElement<Question>
private addCustomError(error: string): SurveyError {
return new CustomError(error, this.survey);
}
public removeError(error: SurveyError): void {
if (!error) return;
var errors = this.errors;
var index = errors.indexOf(error);
public removeError(error: SurveyError): boolean {
if (!error) return false;
const errors = this.errors;
const index = errors.indexOf(error);
if (index !== -1) errors.splice(index, 1);
return index !== -1;
}
private checkForErrors(isOnValueChanged: boolean, fireCallback: boolean): Array<SurveyError> {
var qErrors = new Array<SurveyError>();
Expand Down
8 changes: 5 additions & 3 deletions packages/survey-core/src/question_matrixdropdownbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ export class QuestionMatrixDropdownModelBase extends QuestionMatrixBaseModel<Mat
if(duplicatedRows.indexOf(row) < 0) {
const question = row.getQuestionByName(columnName);
if(question) {
this.removeDuplicationError(question);
this.removeDuplicationError(row, question);
}
}
});
Expand All @@ -2246,8 +2246,10 @@ export class QuestionMatrixDropdownModelBase extends QuestionMatrixBaseModel<Mat
question.addError(new KeyDuplicationError(this.keyDuplicationError, this));
}
}
private removeDuplicationError(question: Question) {
question.removeError(this.getDuplicationError(question));
private removeDuplicationError(row: MatrixDropdownRowModelBase, question: Question) {
if(question.removeError(this.getDuplicationError(question)) && question.errors.length === 0 && !!row.editingObj) {
row.editingObj[question.getValueName()] = question.value;
}
}
public getFirstQuestionToFocus(withError: boolean): Question {
return this.getFirstCellQuestion(withError);
Expand Down
27 changes: 27 additions & 0 deletions packages/survey-core/tests/editingObjectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,33 @@ QUnit.test("Edit choices in matrix dynamic column, check errors", function (asse
assert.equal(row0Q.errors.length, 0, "row0Q errors, #4");
assert.equal(row1Q.errors.length, 0, "row1Q errors, #4");
});
QUnit.test("Edit choices in matrix dynamic column, set correct value", function (assert) {
const question = new QuestionDropdownModel("q1");
question.choices = ["Item1", "Item2", "Item3"];
const survey = new SurveyModel({
elements: [
{
type: "matrixdynamic",
name: "choices",
rowCount: 0,
keyName: "value",
columns: [
{ cellType: "text", name: "value", isRequired: true, isUnique: true }
]
},
],
checkErrorsMode: "onValueChanging"
});
survey.editingObj = question;
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("choices");
assert.equal(matrix.rowCount, 3, "There are 3 items");
const rows = matrix.visibleRows;
rows[1].cells[0].question.value = "Item1";
rows[0].cells[0].question.value = "Item2";
assert.equal(question.choices[0].value, "Item2", "choice 0");
assert.equal(question.choices[1].value, "Item1", "choice 1");
});

QUnit.test("Edit question.page property", function (assert) {
var questionSurvey = new SurveyModel();
questionSurvey.addNewPage("page1");
Expand Down

0 comments on commit 4aaa59a

Please sign in to comment.