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

With checkErrorsMode: "onValueChanged", The validation message doesn'… #8546

Merged
merged 1 commit into from
Jul 11, 2024
Merged
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
1 change: 1 addition & 0 deletions src/base-interfaces.ts
Original file line number Diff line number Diff line change
@@ -333,6 +333,7 @@ export interface IPanel extends ISurveyElement, IParentElement {
elements: Array<IElement>;
ensureRowsVisibility(): void;
validateContainerOnly(): void;
onQuestionValueChanged(el: IElement): void;
}
export interface IPage extends IPanel, IConditionRunner {
isStartPage: boolean;
15 changes: 15 additions & 0 deletions src/panel.ts
Original file line number Diff line number Diff line change
@@ -880,6 +880,21 @@ export class PanelModelBase extends SurveyElement<Question>
this.parent.validateContainerOnly();
}
}
onQuestionValueChanged(el: IElement): void {
const index = this.questions.indexOf(<any>el);
if(index < 0) return;
const dif = 5;
const max = this.questions.length - 1;
const start = index - dif > 0 ? index - dif : 0;
const end = index + dif < max ? index + dif : max;
for(let i = start; i <= end; i ++) {
if(i === index) continue;
const q = this.questions[i];
if(q.errors.length > 0 && q.validate(false)) {
q.validate(true);
}
}
}
private hasErrorsInPanels(rec: any): void {
var errors = <Array<any>>[];
this.hasRequiredError(rec, errors);
3 changes: 3 additions & 0 deletions src/question.ts
Original file line number Diff line number Diff line change
@@ -2253,6 +2253,9 @@ export class Question extends SurveyElement<Question>
this.updateQuestionCss();
}
this.isOldAnswered = undefined;
if(this.parent) {
this.parent.onQuestionValueChanged(this);
}
}
private checkIsValueCorrect(val: any): boolean {
const res = this.isValueEmpty(val, !this.allowSpaceAsAnswer) || this.isNewValueCorrect(val);
3 changes: 3 additions & 0 deletions src/question_custom.ts
Original file line number Diff line number Diff line change
@@ -759,6 +759,9 @@ export abstract class QuestionCustomModelBase extends Question
validateContainerOnly(): void {
// do nothing
}
onQuestionValueChanged(el: IElement): void {
// do nothing
}
getQuestionErrorLocation(): string {
return this.getErrorLocation();
}
3 changes: 3 additions & 0 deletions src/question_multipletext.ts
Original file line number Diff line number Diff line change
@@ -832,6 +832,9 @@ export class QuestionMultipleTextModel extends Question
validateContainerOnly(): void {
// do nothing
}
onQuestionValueChanged(el: IElement): void {
// do nothing
}
public getItemLabelCss(item: MultipleTextItemModel): string {
return new CssClassBuilder()
.append(this.cssClasses.itemLabel)
58 changes: 58 additions & 0 deletions tests/paneltests.ts
Original file line number Diff line number Diff line change
@@ -2814,4 +2814,62 @@ QUnit.test("panel check title in design mode", function (assert) {
assert.equal(panel.cssTitle, "sv_p_title sv_p_title--hidden");
survey.css.panel.titleHidden = oldCss;
survey.css.root = oldRootCss;
});
QUnit.test("Check if errors disappered in the closest questions on changing the question, checkErrorsMode: 'onValueChanged', Bug#8539", function (assert) {
const survey = new SurveyModel({
checkErrorsMode: "onValueChanged",
elements: [
{
type: "text", name: "q1",
validators: [{ type: "expression", expression: "{q1} + {q2} <= 10" }]
},
{ type: "text", name: "q3" },
{
type: "text", name: "q2",
validators: [{ type: "expression", expression: "{q1} + {q2} <= 10" }]
}
]
});
const q1 = survey.getQuestionByName("q1");
const q2 = survey.getQuestionByName("q2");
q1.value = 5;
q2.value = 6;
assert.equal(q1.errors.length, 0, "q1.errors #1");
assert.equal(q2.errors.length, 1, "q2.errors #1");
q1.value = 3;
assert.equal(q1.errors.length, 0, "q1.errors #2");
assert.equal(q2.errors.length, 0, "q2.errors #2");
q1.value = 7;
assert.equal(q1.errors.length, 1, "q1.errors #3");
assert.equal(q2.errors.length, 0, "q2.errors #3");
q2.value = 2;
assert.equal(q1.errors.length, 0, "q1.errors #4");
assert.equal(q2.errors.length, 0, "q2.errors #4");
});
QUnit.test("Check if errors disappered in the closest questions on changing the question, Bug#8539", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "text", name: "q1",
validators: [{ type: "expression", expression: "{q1} + {q2} <= 10" }]
},
{ type: "text", name: "q3" },
{
type: "text", name: "q2",
validators: [{ type: "expression", expression: "{q1} + {q2} <= 10" }]
}
]
});
const q1 = survey.getQuestionByName("q1");
const q2 = survey.getQuestionByName("q2");
q1.value = 5;
q2.value = 6;
assert.equal(q1.errors.length, 0, "q1.errors #1");
assert.equal(q2.errors.length, 0, "q2.errors #1");
assert.equal(survey.completeLastPage(), false, "Could not complete");
assert.equal(q1.errors.length, 1, "q1.errors #2");
assert.equal(q2.errors.length, 1, "q2.errors #2");
q1.value = 1;
assert.equal(q1.errors.length, 0, "q1.errors #3");
assert.equal(q2.errors.length, 0, "q2.errors #3");
});
34 changes: 34 additions & 0 deletions tests/question_matrixdropdownbasetests.ts
Original file line number Diff line number Diff line change
@@ -1496,4 +1496,38 @@ QUnit.test("showInMultipleColumns & random choices, Bug#8348", function (assert)
assert.equal(rows[0].cells[1].question.visibleChoices[0].value, "col5", "row1 question");
assert.equal(rows[1].cells[1].question.visibleChoices[0].value, "col5", "row2 question");
Helpers.randomizeArray = oldFunc;
});
QUnit.test("Check if errors disappered in the cells in the current row on changing cell value, Bug#8539", function (assert) {
const survey = new SurveyModel({
elements: [
{
type: "matrixdynamic",
name: "matrix",
rowCount: 1,
columns: [
{
name: "col1", cellType: "text",
validators: [{ type: "expression", expression: "{row.col1} + {row.col2} <= 10" }]
},
{ name: "col3", cellType: "text" },
{
name: "col2", cellType: "text",
validators: [{ type: "expression", expression: "{row.col1} + {row.col2} <= 10" }]
}
]
}
]
});
const matrix = <QuestionMatrixDynamicModel>survey.getQuestionByName("matrix");
const row = matrix.visibleRows[0];
const q1 = row.cells[0].question;
const q2 = row.cells[2].question;
q1.value = 7;
q2.value = 7;
matrix.validate(true);
assert.equal(q1.errors.length, 1, "q1 errors #1");
assert.equal(q2.errors.length, 1, "q2 errors #1");
q2.value = 1;
assert.equal(q1.errors.length, 0, "q1 errors #2");
assert.equal(q2.errors.length, 0, "q2 errors #2");
});