Skip to content

Conversation

@zoul0813
Copy link
Member

fixes #358 - support "validateBeforeSubmit" with async validators

  • added validateAsync (default: false) form option
  • added isAsync parameter to FormGenerator.validate()
  • FormGenerator.validate() returns 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

To use the new async validation logic, just set validateAsync: true in your form options, or call FormGenerator.validate(true) manually.

Code should be backwards compatible. Additional tests are likely needed to test the new async logic.

…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
validate(calledParent) {
this.clearValidationErrors();
let validateAsync = objGet(this.formOptions, "validateAsync", false);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need the validateAsync option? Is it not enough if we check the type of result of validator function (Promise or not)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated code returns a promise if validateAsync is true, and waits for async validation to complete ... this is not what the current code does, so the new option is a way to enable it while maintaining backwards compatibility so we don’t break forms that expect the validate function to return a Boolean.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

V3 would likely remove this option and always return a promise from validate... making it the default validation mode, which is what most people are likely expecting from the current async validation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. in v3 we change it to easier (handle all as Promise)

@icebob icebob merged commit 56c075f into vue-generators:master Dec 16, 2017
@zoul0813 zoul0813 deleted the feature/358-async-validation branch July 11, 2018 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asynchronous Validators are broken

2 participants