Skip to content
This repository has been archived by the owner on Dec 25, 2017. It is now read-only.

Commit

Permalink
feat(async): support async validation error message
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 6, 2016
1 parent b70de4d commit 7e65517
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ export default class BaseValidation {

if (validator) {
let value = this._getValue(this._el)
this._invokeValidator(this._vm, validator, value, descriptor.arg, (ret) => {
this._invokeValidator(this._vm, validator, value, descriptor.arg, (ret, err) => {
if (!ret) {
valid = false
if (msg) {
if (err) { // async error message
errors.push({ validator: name, message: err })
results[name] = err
} else if (msg) {
let error = { validator: name }
error.message = typeof msg === 'function'
? msg.call(this._vm, this.field, descriptor.arg)
Expand Down Expand Up @@ -273,15 +276,17 @@ export default class BaseValidation {
for (let i = 0, l = cbs.length; i < l; i++) {
cbs[i](true)
}
}, () => { // reject
cb(false)
}, (msg) => { // reject
cb(false, msg)
})
}
} else if (isPromise(future)) { // promise
future.then(() => { // resolve
cb(true)
}).catch(() => { // reject
cb(false)
}, (msg) => { // reject
cb(false, msg)
}).catch((err) => {
cb(false, err.message)
})
} else { // sync
cb(future)
Expand Down
12 changes: 6 additions & 6 deletions test/specs/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('async', () => {
exist: (val) => {
return (resolve, reject) => {
setTimeout(() => {
reject()
reject('already registration !!')
}, DELAY)
}
}
Expand All @@ -86,7 +86,7 @@ describe('async', () => {
it('should be validated', (done) => {
setTimeout(() => {
// default
assert(vm.$validator1.username.exist === true)
assert(vm.$validator1.username.exist === 'already registration !!')
assert(vm.$validator1.valid === false)
assert(vm.$validator1.touched === false)
assert(vm.$validator1.dirty === false)
Expand All @@ -98,7 +98,7 @@ describe('async', () => {
trigger(input, 'input')
trigger(input, 'blur')
setTimeout(() => {
assert(vm.$validator1.username.exist === true)
assert(vm.$validator1.username.exist === 'already registration !!')
assert(vm.$validator1.valid === false)
assert(vm.$validator1.touched === true)
assert(vm.$validator1.dirty === true)
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('async', () => {
exist: (val) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject()
reject('already registration !!')
}, DELAY)
})
}
Expand All @@ -185,7 +185,7 @@ describe('async', () => {
it('should be validated', (done) => {
setTimeout(() => {
// default
assert(vm.$validator1.username.exist === true)
assert(vm.$validator1.username.exist === 'already registration !!')
assert(vm.$validator1.valid === false)
assert(vm.$validator1.touched === false)
assert(vm.$validator1.dirty === false)
Expand All @@ -197,7 +197,7 @@ describe('async', () => {
trigger(input, 'input')
trigger(input, 'blur')
setTimeout(() => {
assert(vm.$validator1.username.exist === true)
assert(vm.$validator1.username.exist === 'already registration !!')
assert(vm.$validator1.valid === false)
assert(vm.$validator1.touched === true)
assert(vm.$validator1.dirty === true)
Expand Down

0 comments on commit 7e65517

Please sign in to comment.