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

Commit

Permalink
🐛 bug(reset): fix cannot reset with using initial params and v-model
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Sep 9, 2016
1 parent 5031198 commit 4c6c793
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 139 deletions.
24 changes: 9 additions & 15 deletions src/directives/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,16 @@ export default function (Vue) {
if (!value || this._invalid) { return }

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

let options = { field: this.field, noopable: this._initialNoopValidation }
let options = { field: this.field }
if (this.frag) {
options.el = this.frag.node
}
this.validator.validate(options)

if (this._initialNoopValidation) {
this._initialNoopValidation = null
}
},

unbind () {
Expand Down Expand Up @@ -152,8 +148,6 @@ export default function (Vue) {

params.group
&& validator.addGroupValidation(params.group, this.field)

this._initialNoopValidation = this.isInitialNoopValidation(params.initial)
},

listen () {
Expand Down Expand Up @@ -242,26 +236,26 @@ export default function (Vue) {
this.anchor = null
},

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

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

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

each(value, (val, key) => {
if (isPlainObject(val)) {
if ('rule' in val) {
let msg = 'message' in val ? val.message : null
let initial = 'initial' in val ? val.initial : null
this.validation.setValidation(key, val.rule, msg, initial)
let init = 'initial' in val ? val.initial : null
this.validation.setValidation(key, val.rule, msg, init || initial)
}
} else {
this.validation.setValidation(key, val)
this.validation.setValidation(key, val, undefined, initial)
}
})
},
Expand Down
14 changes: 9 additions & 5 deletions src/validations/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ export default class BaseValidation {
})
}

resetValidationNoopable () {
each(this._validators, (descriptor, key) => {
if (descriptor.initial && !descriptor._isNoopable) {
descriptor._isNoopable = true
}
})
}

setValidation (name, arg, msg, initial) {
let validator = this._validators[name]
if (!validator) {
Expand Down Expand Up @@ -243,11 +251,7 @@ export default class BaseValidation {
}

reset () {
each(this._validators, (descriptor, key) => {
if (descriptor.initial && !descriptor._isNoopable) {
descriptor._isNoopable = true
}
})
this.resetValidationNoopable()
this.resetFlags()
this._init = this._getValue(this._el)
}
Expand Down
1 change: 1 addition & 0 deletions src/validations/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class CheckboxValidation extends BaseValidation {
}

reset () {
this.resetValidationNoopable()
this.resetFlags()
each(this._inits, (item, index) => {
item.init = item.el.checked
Expand Down
1 change: 1 addition & 0 deletions src/validations/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default class RadioValidation extends BaseValidation {
}

reset () {
this.resetValidationNoopable()
this.resetFlags()
each(this._inits, (item, index) => {
item.init = item.el.checked
Expand Down
4 changes: 0 additions & 4 deletions src/validations/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export default class SelectValidation extends BaseValidation {
this._unwatch && this._unwatch()
}

reset () {
this.resetFlags()
}

_getValue (el) {
let ret = []

Expand Down
4 changes: 2 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default class Validator {
delete vm['$setValidationErrors']
vm.$validate = null
delete vm['$validate']
vm.$validatorReset = null
delete vm['$validatorReset']
vm.$resetValidation = null
delete vm['$resetValidation']
vm._validatorMaps[this.name] = null
delete vm._validatorMaps[this.name]
vm[this.name] = null
Expand Down
Loading

0 comments on commit 4c6c793

Please sign in to comment.