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

Asynchronous Validators are broken #358

Closed
arnKo opened this issue Dec 7, 2017 · 2 comments · Fixed by #366
Closed

Asynchronous Validators are broken #358

arnKo opened this issue Dec 7, 2017 · 2 comments · Fixed by #366

Comments

@arnKo
Copy link

arnKo commented Dec 7, 2017

I found two major breaks in the asynchronous validation. I reproduced both in this jsfiddle

First, if I type multiple characters into a text field, for each typed character a new validation error is displayed. Even if using formOptions.validateDebounceTime, which is currently not documented, multiple validation errors are displayed if I type a new character after the debounce timer and before the asynchronous validation is completed.

Second, asynchronous validation errors are not included when using a submit button with the following schema:

{
    type: 'submit',
    validateBeforeSubmit: true,
}

I can still submit the form even if asynchronous validation errors are currently displayed.

@zoul0813
Copy link
Member

I think I have a solution for this, using Promise.all() which will basically make all validations "promises" ... some will just resolve immediately, while others will wait for the promise to resolve.

in FormGenerator.vue, I'm modifying the validate() method to look something like this:

validate() {
  this.clearValidationErrors();

  let fields = [];
  let results = [];

  forEach(this.$children, (child) => {
    if (isFunction(child.validate)) {
      fields.push(child); // keep track of validated children
      results.push(child.validate(true));
    }
  });

  return Promise.all(results).then((errors) => {
    let formErrors = [];
    forEach(errors, (err, i) => {
      if(isArray(err) && err.length > 0) {
        formErrors.push({
          field: fields[i],
          error: err
        });
      }
    })
    this.errors = formErrors;
    let isValid = formErrors.length == 0;
    this.$emit("validated", isValid, formErrors);
    return formErrors;
  });
}

Should have a PR for this by the end of the day.

zoul0813 added a commit to zoul0813/vue-form-generator that referenced this issue Dec 15, 2017
…validators

* added `validateAsync` (default: false) form option
* added `isAsync` parameter to FormGenerator.validate()
* FormGenerator.validate() and AbstractField.validate() return Promise when `validateAsync` or `isAsync` is true
* renamed `click` handler to `onClick` in FieldSubmit
* added `onValidationError(model, schema, errors)` to FieldSubmit schema to handle validation errors
* added async validator support for FieldSupport
* changed `each` to `forEach` in various places, as "each" is an alias to "forEach" and "forEach" looks more explicit
* removed call to Vue.util.hyphenate as this is no longer supported by Vue, replaced with equivalent `String.replace` expression
* updated fieldSubmit.spec to add "valid form" and "invalid form" tests, valid forms will always call `onSubmit`, invalid forms will not call `onSubmit` (when validateBeforeSubmit = true)
* various code clean up
icebob added a commit that referenced this issue Dec 16, 2017
fixes #358 - support "validateBeforeSubmit" with async validators
@zoul0813
Copy link
Member

@arnKo that PR solves this issue, but to maintain backwards compatibility you’ll have to add “validayeAsyc: true” to your formOptions being passed to VFG. That will tell VFG to wait on async validators before calling the onSubmit callback, and the VFG validate method will return a Promise instead of a Boolean.

zoul0813 added a commit to zoul0813/vue-form-generator that referenced this issue Jan 8, 2018
* master:
  Update README.md
  Update README.md
  Update badges
  removed commented out console.log statements
  fixes vue-generators#358 - support "validateBeforeSubmit" with async validators

# Conflicts:
#	src/fields/core/fieldSubmit.vue
#	test/unit/specs/util.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants