kazupon / vue-validator

:white_check_mark: Validator component for Vue.js
MIT License
2.19k stars 431 forks source link

$validate when using async validation #253

Closed erguotou520 closed 8 years ago

erguotou520 commented 8 years ago

vue & vue-validator version

1.0.24, 2.1.3

Steps to reproduce

  1. A form validation
  2. A field with aysnc validation
  3. A button to save the form
  4. The save function looks like: js function save() { this.$validate(true); if (this.$validation.valid) { // do save } }
  5. Click the save button

    What is Expected?

Can't save the form because there is a field that is invalid.

What is actually happening?

Success post the save data because the invalid field needs async validate, when click the save button, the async result not returned.

JSFiddle

A demo here https://jsfiddle.net/k0h3m8aq/1/ Typed something, blur, then type unique, click save button quickly, nothing. Wait for a while, click button, the alert shows.

My puzzle

The $validate returns undefined, so it's sync, this makes the question. Can it return a promise so I can submit the form after resolve? Or? There is no need to do unique validation in this case? Do it in backend?

kazupon commented 8 years ago

In current version, The $validate don't return the promise. However, You can use the cb argument. See the document. http://vuejs.github.io/vue-validator/en/api.html#validate-field-touched-cb- (sorry, bad sample ...)

the below demo (fork the your demo and updated): https://jsfiddle.net/kazupon/7pxyayqj/2/

erguotou520 commented 8 years ago

Ok, got it. The cb did the same. I didn't see this argument before....