Skip to content

Commit

Permalink
Fix the dublicating error list: surveyjs#668
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Sep 19, 2017
1 parent 692ffb9 commit 6c3573b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ export class Question extends QuestionBase implements IValidatorOwner {
if (this.errors.length == 0 && !this.isEmpty()) {
var error = this.runValidators();
if (error) {
//validators may change the question value.
this.errors = [];
this.errors.push(error);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/question_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class QuestionFileModel extends Question {
protected onCheckForErrors(errors: Array<SurveyError>) {
super.onCheckForErrors(errors);
if (this.isUploading) {
this.errors.push(new CustomError(surveyLocalization.getString("uploadingFile")));
errors.push(new CustomError(surveyLocalization.getString("uploadingFile")));
}
}
private checkFileForErrors(file: File): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/question_matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class QuestionMatrixModel extends Question implements IMatrixData {
protected onCheckForErrors(errors: Array<SurveyError>) {
super.onCheckForErrors(errors);
if (this.hasErrorInRows()) {
this.errors.push(new CustomError(surveyLocalization.getString("requiredInAllRowsError")));
errors.push(new CustomError(surveyLocalization.getString("requiredInAllRowsError")));
}
}
private hasErrorInRows(): boolean {
Expand Down
15 changes: 3 additions & 12 deletions src/question_multipletext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,23 +231,14 @@ export class QuestionMultipleTextModel extends Question implements IMultipleText
}
return null;
}
public hasErrors(fireCallback: boolean = true): boolean {
var res = super.hasErrors(fireCallback);
if(!res) res = this.hasErrorInItems(fireCallback);
return res;
}
protected hasErrorInItems(fireCallback: boolean): boolean {
protected onCheckForErrors(errors: Array<SurveyError>) {
super.onCheckForErrors(errors);
for(var i = 0; i < this.items.length; i ++) {
var item = this.items[i];
if(item.isRequired && !item.value) {
this.errors.push(new AnswerRequiredError());
if(fireCallback) {
this.fireCallback(this.errorsChangedCallback);
}
return true;
errors.push(new AnswerRequiredError());
}
}
return false;
}
//IMultipleTextData
getMultipleTextValue(name: string) {
Expand Down
18 changes: 15 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
this.textPreProcessor = new TextPreProcessor();
this.textPreProcessor.onHasValue = function (name: string) { return self.hasProcessedTextValue(name); };
this.textPreProcessor.onProcess = function (name: string, returnDisplayValue: boolean) { return self.getProcessedTextValue(name, returnDisplayValue); };
this.pagesValue = this.createNewArray("pages", function(value){ value.setSurveyImpl(self); });
this.pagesValue = this.createNewArray("pages", function(value){ self.onPageAdded(value); });
this.triggersValue = this.createNewArray("triggers", function(value){ value.setOwner(self); });
this.updateProcessedTextValues();
this.onBeforeCreating();
Expand Down Expand Up @@ -1106,11 +1106,11 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
this.updateVisibleIndexes();
}
/**
* Creates a new page and adds it into the survey
* Creates a new page and adds it into the survey. Genarates a new name if the name parameter is not set.
* @param name a page name
* @see addPage
*/
public addNewPage(name: string) {
public addNewPage(name: string = null) {
var page = this.createNewPage(name);
this.addPage(page);
return page;
Expand Down Expand Up @@ -1546,6 +1546,17 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
if (newValue === null || oldValue === null) return newValue === oldValue;
return this.isTwoValueEquals(newValue, oldValue);
}
private onPageAdded(page: PageModel) {
page.setSurveyImpl(this);
if(!page.name) page.name = this.generateNewName(this.pages, "page");
}
private generateNewName(elements: Array<any>, baseName: string): string {
var keys = {};
for(var i = 0; i < elements.length; i ++) keys[elements[i]["name"]] = true;
var index = 1;
while(keys[baseName + index]) index ++;
return baseName + index;
}
protected tryGoNextPageAutomatic(name: string) {
if (!this.goNextPageAutomatic || !this.currentPage) return;
var question = this.getQuestionByName(name);
Expand Down Expand Up @@ -1616,6 +1627,7 @@ export class SurveyModel extends Base implements ISurvey, ISurveyData, ISurveyIm
questionAdded(question: IQuestion, index: number, parentPanel: any, rootPanel: any) {
this.updateVisibleIndexes();
this.addQuestionToProcessedTextValues(question);
if(!question.name) question.name = this.generateNewName(this.getAllQuestions(), "question");
this.onQuestionAdded.fire(this, { 'question': question, 'name': question.name, 'index': index, 'parentPanel': parentPanel, 'rootPanel': rootPanel });
}
questionRemoved(question: IQuestion) {
Expand Down
31 changes: 30 additions & 1 deletion tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Question} from "../src/question";
import {QuestionHtmlModel} from "../src/question_html";
import {SurveyTriggerVisible, SurveyTriggerComplete, SurveyTriggerSetValue} from "../src/trigger";
import {surveyLocalization} from "../src/surveyStrings";
import {EmailValidator} from "../src/validator";
import {EmailValidator, NumericValidator} from "../src/validator";
import {JsonObject} from "../src/jsonobject";
import {QuestionTextModel} from "../src/question_text";
import {QuestionMultipleTextModel, MultipleTextItemModel} from "../src/question_multipletext";
Expand Down Expand Up @@ -1637,6 +1637,35 @@ QUnit.test("matrixdynamic.defaultValue - check the complex property", function (
assert.deepEqual(survey.getValue("matrix"), [{col1: 1, col2: 2}, {col1: 3, col2: 4}], "set complex defaultValue correctly");
});

QUnit.test("Dublicate errors", function (assert) {
var survey = new SurveyModel();
survey.addNewPage("p1");
var q = <QuestionTextModel>survey.pages[0].addNewQuestion("text", "q1");
q.validators.push(new NumericValidator(0, 25));
var valueChangedCounter = 0;
survey.onValueChanged.add(function(s, options){
valueChangedCounter ++;
options.question.hasErrors(true);
});
assert.equal(q.errors.length, 0, "There is no errors so far");
q.value = "26";
assert.equal(q.errors.length, 1, "There should be one error");
assert.equal(valueChangedCounter, 1, "on value changed called one time");
assert.equal(q.value, 26, "the value is 26");
});


QUnit.test("Auto generate names for question/panel/page", function (assert) {
var survey = new SurveyModel();
survey.addNewPage();
assert.equal(survey.pages[0].name, "page1", "the first name is page1");
survey.addNewPage();
assert.equal(survey.pages[1].name, "page2", "the second name is page2");
survey.pages[0].name = "newpage"
survey.addNewPage();
assert.equal(survey.pages[2].name, "page1", "the third name is page1 again");
});


function twoPageSimplestSurvey() {
var survey = new SurveyModel();
Expand Down

0 comments on commit 6c3573b

Please sign in to comment.