surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.01k stars 782 forks source link

hasErrors() functions unaware of errors added manually by question.addError() #5435

Open metaturso opened 1 year ago

metaturso commented 1 year ago

Are you requesting a feature, reporting a bug or asking a question?

This is a bug report.

What is the current behavior?

Calling question.hasErrors() after adding one or more errors by calling question.addErrors() results in the custom errors being lost.

All variants of hasErrors() are only aware of the SurveyJS errors produced by validators.

What is the expected behavior?

My expectation is that calling question.hasErrors(), survey.hasPageErrors(), survey.hasCurrentPageErrors(), or survey.hasErrors() should preserve custom errors manually added to a question by calling addError().

How would you reproduce the current behavior (if this is a bug)?

Obtain the instance of a SurveyJS question and call addError() before calling any of the hasErrors() methods available.

Provide the test code and the tested page URL (if applicable)

Tested page URL: https://codesandbox.io/s/frosty-grass-cipyl9?file=/src/App.vue

Test code

const page = this.survey.activePage;
const question = this.survey.getQuestionByName("LastName");
question.addError("This is an error.");

console.log(`question has error sync result`, question.hasErrors());
console.log(`question has error async handle`, question.hasErrors((hasErrors) => {
  console.log(`question has error async result`, hasErrors);
}));

console.log(`page error sync result`, this.survey.hasPageErrors(page));

question.addError("This is another error.");
console.log(`survey has error sync result`, this.survey.hasErrors());
console.log(`survey has error async handle`, this.survey.hasErrors((hasErrors) => {
  console.log(`survey has error async result`, hasErrors);
}));

Specify your

andrewtelnov commented 1 year ago

@metaturso Your custom errors will be removed on every calling of hasError function. You need to use survey.onSettingQuestionErrors event to add your custom error.

Thank you, Andrew

metaturso commented 1 year ago

Marvellous. Thanks very much for the info, @andrewtelnov, that looks exactly like what I need.

metaturso commented 1 year ago

Hi @andrewtelnov, I apologize for reopening this. Would you mind suggesting an alternative to survey.onSettingQuestionErrors that supports async validation?

I understand SurveyJS has technical limitations that prevent it from supporting async validation, which I had originally hoped could be worked around by deferring the call to hasErrors until after the validation results had been attached to the question. Alas, as you pointed out, the errors are cleared from the question upon calling hasErrors.

I've asked a friend to have a look at this and we both agree that onSettingQuestionErrors isn't going to work. It can't be used to validate a question whose validation status is determined by an async call.

What do you suggest other than calling onServerValidateQuestions which, as described in #5427, cannot be used in a scenarios that involve validating the survey without triggering the undesirable and unchangeable default "on complete" behaviour?