Skip to content

Commit

Permalink
A Text input with the "date" type removes an invalid input value (02/…
Browse files Browse the repository at this point in the history
…30/2000) before the survey.onValidateQuestion event occurs fix #8617
  • Loading branch information
andrewtelnov committed Jul 26, 2024
1 parent d1122f2 commit c5876a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/question_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ export class QuestionTextModel extends QuestionTextBase {
}
return super.valueFromDataCore(val);
}
protected onCheckForErrors(
errors: Array<SurveyError>,
isOnValueChanged: boolean
) {
private dateValidationMessage: string;
protected onCheckForErrors(errors: Array<SurveyError>, isOnValueChanged: boolean): void {
super.onCheckForErrors(errors, isOnValueChanged);
if (isOnValueChanged) return;
if (this.isValueLessMin) {
Expand Down Expand Up @@ -398,6 +396,9 @@ export class QuestionTextModel extends QuestionTextBase {
); };
errors.push(maxError);
}
if(!!this.dateValidationMessage) {
errors.push(new CustomError(this.dateValidationMessage, this));
}

const valName = this.getValidatorTitle();
var emailValidator = new EmailValidator();
Expand Down Expand Up @@ -535,8 +536,11 @@ export class QuestionTextModel extends QuestionTextBase {
this.supportGoNextPageError()
);
}
protected setNewValue(newValue: any) {
protected setNewValue(newValue: any): void {
newValue = this.correctValueType(newValue);
if(!!newValue) {
this.dateValidationMessage = undefined;
}
super.setNewValue(newValue);
}
protected correctValueType(newValue: any): any {
Expand Down Expand Up @@ -605,14 +609,19 @@ export class QuestionTextModel extends QuestionTextBase {
}
this.updateRemainingCharacterCounter(event.target.value);
};
private updateDateValidationMessage(event: any): void {
this.dateValidationMessage = this.isDateInputType && !!event.target ? event.target.validationMessage : undefined;
}
public onKeyDown = (event: any) => {
this.updateDateValidationMessage(event);
this.onKeyDownPreprocess && this.onKeyDownPreprocess(event);
if (this.isInputTextUpdate) {
this._isWaitingForEnter = event.keyCode === 229;
}
this.onTextKeyDownHandler(event);
}
public onChange = (event: any): void => {
this.updateDateValidationMessage(event);
const elementIsFocused = event.target === settings.environment.root.activeElement;
if (elementIsFocused) {
if (this.isInputTextUpdate) {
Expand Down
20 changes: 20 additions & 0 deletions tests/question_texttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,23 @@ QUnit.test("inputType='month' and today function, #8552", function(assert) {
const etalon = new Date().getFullYear() + "-" + (new Date().getMonth() + 1);
assert.equal(q1.value, etalon, "today works correctly for month input");
});
QUnit.test("inputType='date' invalid value, #8617", function(assert) {
const survey = new SurveyModel({
"elements": [
{ "type": "text", "name": "q1", "inputType": "date" },
]
});
const q1 = <QuestionTextModel>survey.getQuestionByName("q1");
q1.value = "2000-01-01";
assert.equal(q1.errors.length, 0, "errors #1");
const event = { target: { value: "", validationMessage: "Invalid date" } };
q1.onKeyDown(event);
q1.value = undefined;
assert.equal(q1.errors.length, 0, "errors #2");
survey.completeLastPage();
assert.equal(q1.errors.length, 1, "errors #3");
assert.equal(q1.errors[0].text, "Invalid date", "errors #4");
assert.equal(survey.state, "running", "survey.state #1");
q1.value = "2000-01-01";
assert.equal(q1.errors.length, 0, "errors #5");
});

0 comments on commit c5876a4

Please sign in to comment.