Skip to content

Commit

Permalink
Merge pull request #5315 from surveyjs/features/5304-survey-keepincor…
Browse files Browse the repository at this point in the history
…rectvalues

Add keepIncorrectValues on Survey level fixed #5304
  • Loading branch information
andrewtelnov authored Dec 7, 2022
2 parents df292ef + 9acf85a commit c8b5dab
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
isClearValueOnHidden: boolean;
isClearValueOnHiddenContainer: boolean;
questionsOrder: string;
keepIncorrectValues: boolean;
questionCreated(question: IQuestion): any;
questionAdded(
question: IQuestion,
Expand Down
7 changes: 7 additions & 0 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,12 @@ export class QuestionSelectBase extends Question {
public set hideIfChoicesEmpty(val: boolean) {
this.setPropertyValue("hideIfChoicesEmpty", val);
}
/**
* Specifies whether to keep values that cannot be assigned to this question, for example, choices unlisted in the `choices` array.
*
* > This property cannot be specified in the survey JSON schema. Use dot notation to specify it.
* @see clearIncorrectValues
*/
public get keepIncorrectValues(): boolean {
return this.getPropertyValue("keepIncorrectValues", false);
}
Expand Down Expand Up @@ -1237,6 +1243,7 @@ export class QuestionSelectBase extends Question {
}
}
protected hasValueToClearIncorrectValues(): boolean {
if(!!this.survey && this.survey.keepIncorrectValues) return false;
return !this.keepIncorrectValues && !this.isEmpty();
}
protected clearValueIfInvisibleCore(): void {
Expand Down
13 changes: 12 additions & 1 deletion src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,18 @@ export class SurveyModel extends SurveyElementCore
key.substring(0, key.indexOf(postPrefix))
);
}

/**
* Specifies whether to keep values that cannot be assigned to questions, for example, choices unlisted in the choices array.
*
* > This property cannot be specified in the survey JSON schema. Use dot notation to specify it.
* @see clearIncorrectValues
*/
public get keepIncorrectValues(): boolean {
return this.getPropertyValue("keepIncorrectValues", false);
}
public set keepIncorrectValues(val: boolean) {
this.setPropertyValue("keepIncorrectValues", val);
}
/**
* Gets or sets the survey locale. The default value it is empty, this means the 'en' locale is used.
* You can set it to 'de' - German, 'fr' - French and so on. The library has built-in localization for several languages. The library has a multi-language support as well.
Expand Down
35 changes: 35 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15758,3 +15758,38 @@ QUnit.test("Expression for questions with a lot of dots #2", function (assert) {
questionWF4_4_1.value = "Yes";
assert.equal(questionWF4_4_1_1.visible, true, "questionWF4_4_1_1 true");
});

QUnit.test("selectbase.keepIncorrectValues & survey.keepIncorrectValues", function (assert) {
const survey = new SurveyModel({
"elements": [
{
"type": "radiogroup",
"name": "q1",
"choices": ["item1", "item2"]
}
]
});
const question = <QuestionRadiogroupModel>survey.getQuestionByName("q1");
question.value = "item3";
survey.clearIncorrectValues();
assert.equal(true, question.isEmpty(), "clear incorrect value, #1");
question.keepIncorrectValues = true;
question.value = "item3";
survey.clearIncorrectValues();
assert.equal("item3", question.value, "keep incorrect value, #2");

question.keepIncorrectValues = false;
survey.clearIncorrectValues();
assert.equal(true, question.isEmpty(), "clear incorrect value, #3");

survey.keepIncorrectValues = true;
question.value = "item3";
survey.clearIncorrectValues();
assert.equal("item3", question.value, "keep incorrect value, #4");

survey.keepIncorrectValues = false;
question.value = "item3";
survey.clearIncorrectValues();
assert.equal(true, question.isEmpty(), "clear incorrect value, #5");
});

0 comments on commit c8b5dab

Please sign in to comment.