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

Commit

Permalink
🐛 bug(validator): fix dynamic custom validator error
Browse files Browse the repository at this point in the history
Closes #274
  • Loading branch information
kazupon committed Jul 19, 2016
1 parent 8de42a7 commit b5d5487
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/directives/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ export default function (Vue) {
update (value, old) {
if (!value || this._invalid) { return }

if (isPlainObject(value)) {
this.handleObject(value)
} else if (Array.isArray(value)) {
this.handleArray(value)
if (isPlainObject(value) || (old && isPlainObject(old))) {
this.handleObject(value, old)
} else if (Array.isArray(value) || (old && Array.isArray(old))) {
this.handleArray(value, old)
}

let options = { field: this.field, noopable: this._initialNoopValidation }
Expand Down Expand Up @@ -242,13 +242,17 @@ export default function (Vue) {
this.anchor = null
},

handleArray (value) {
handleArray (value, old) {
old && this.validation.resetValidation()

each(value, (val) => {
this.validation.setValidation(val)
})
},

handleObject (value) {
handleObject (value, old) {
old && this.validation.resetValidation()

each(value, (val, key) => {
if (isPlainObject(val)) {
if ('rule' in val) {
Expand Down
8 changes: 8 additions & 0 deletions src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export default class BaseValidation {
this._unwatch && this._unwatch()
}

resetValidation () {
const keys = Object.keys(this._validators)
each(keys, (key, index) => {
this._validators[key] = null
delete this._validators[key]
})
}

setValidation (name, arg, msg, initial) {
let validator = this._validators[name]
if (!validator) {
Expand Down
81 changes: 81 additions & 0 deletions test/specs/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,85 @@ describe('github issues', () => {
})
})
})

describe('#274', () => {
beforeEach((done) => {
el.innerHTML = `
<validator name="validation1">
<form novalidate>
<div class="inputs">
<input type="text" id="username" v-validate:username="customeValidator">
<input type="text" id="comment" v-validate:comment="{ maxlength: 4 }">
</div>
<div class="errors">
<p v-if="$validation1.username.required">Required your name.</p>
<p v-if="$validation1.comment.maxlength">Your comment is too long.</p>
</div>
<input type="submit" value="send" v-if="$validation1.valid">
<button id="buttonCustom1" type="button" @click="onCustomValidator1">custom1</button>
<button id="buttonCustom2" type="button" @click="onCustomValidator2">custom2</button>
</form>
</validator>
`
vm = new Vue({
el,
data: {
customeValidator: ['custom1']
},
validators: {
custom1 (val) {
return val === 'custom1'
},
custom2 (val) {
return val === 'custom2'
}
},
methods: {
onCustomValidator1 () {
this.customeValidator = ['custom1']
},
onCustomValidator2 () {
this.customeValidator = ['custom2']
}
}
})
vm.$nextTick(done)
})

it('should be validated', (done) => {
let username = el.querySelector('#username')
let buttonCustom2 = el.querySelector('#buttonCustom2')

assert(vm.$validation1.invalid === true)
assert(vm.$validation1.username.custom1 === true)
assert(vm.$validation1.username.custom2 === undefined)

username.value = 'custom1'
trigger(username, 'input')
trigger(username, 'blur')
vm.$nextTick(() => {
assert(vm.$validation1.valid === true)
assert(vm.$validation1.username.custom1 === false)
assert(vm.$validation1.username.custom2 === undefined)

buttonCustom2.click()
vm.$nextTick(() => {
assert(vm.$validation1.invalid === true)
assert(vm.$validation1.username.custom1 === undefined)
assert(vm.$validation1.username.custom2 === true)

username.value = 'custom2'
trigger(username, 'input')
trigger(username, 'blur')
vm.$nextTick(() => {
assert(vm.$validation1.valid === true)
assert(vm.$validation1.username.custom1 === undefined)
assert(vm.$validation1.username.custom2 === false)

done()
})
})
})
})
})
})

0 comments on commit b5d5487

Please sign in to comment.