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

Commit 4c6c793

Browse files
committed
🐛 bug(reset): fix cannot reset with using initial params and v-model
1 parent 5031198 commit 4c6c793

File tree

8 files changed

+334
-139
lines changed

8 files changed

+334
-139
lines changed

src/directives/validate.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,16 @@ export default function (Vue) {
9999
if (!value || this._invalid) { return }
100100

101101
if (isPlainObject(value) || (old && isPlainObject(old))) {
102-
this.handleObject(value, old)
102+
this.handleObject(value, old, this.params.initial)
103103
} else if (Array.isArray(value) || (old && Array.isArray(old))) {
104-
this.handleArray(value, old)
104+
this.handleArray(value, old, this.params.initial)
105105
}
106106

107-
let options = { field: this.field, noopable: this._initialNoopValidation }
107+
let options = { field: this.field }
108108
if (this.frag) {
109109
options.el = this.frag.node
110110
}
111111
this.validator.validate(options)
112-
113-
if (this._initialNoopValidation) {
114-
this._initialNoopValidation = null
115-
}
116112
},
117113

118114
unbind () {
@@ -152,8 +148,6 @@ export default function (Vue) {
152148

153149
params.group
154150
&& validator.addGroupValidation(params.group, this.field)
155-
156-
this._initialNoopValidation = this.isInitialNoopValidation(params.initial)
157151
},
158152

159153
listen () {
@@ -242,26 +236,26 @@ export default function (Vue) {
242236
this.anchor = null
243237
},
244238

245-
handleArray (value, old) {
239+
handleArray (value, old, initial) {
246240
old && this.validation.resetValidation()
247241

248242
each(value, (val) => {
249-
this.validation.setValidation(val)
243+
this.validation.setValidation(val, undefined, undefined, initial)
250244
})
251245
},
252246

253-
handleObject (value, old) {
247+
handleObject (value, old, initial) {
254248
old && this.validation.resetValidation()
255249

256250
each(value, (val, key) => {
257251
if (isPlainObject(val)) {
258252
if ('rule' in val) {
259253
let msg = 'message' in val ? val.message : null
260-
let initial = 'initial' in val ? val.initial : null
261-
this.validation.setValidation(key, val.rule, msg, initial)
254+
let init = 'initial' in val ? val.initial : null
255+
this.validation.setValidation(key, val.rule, msg, init || initial)
262256
}
263257
} else {
264-
this.validation.setValidation(key, val)
258+
this.validation.setValidation(key, val, undefined, initial)
265259
}
266260
})
267261
},

src/validations/base.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ export default class BaseValidation {
7979
})
8080
}
8181

82+
resetValidationNoopable () {
83+
each(this._validators, (descriptor, key) => {
84+
if (descriptor.initial && !descriptor._isNoopable) {
85+
descriptor._isNoopable = true
86+
}
87+
})
88+
}
89+
8290
setValidation (name, arg, msg, initial) {
8391
let validator = this._validators[name]
8492
if (!validator) {
@@ -243,11 +251,7 @@ export default class BaseValidation {
243251
}
244252

245253
reset () {
246-
each(this._validators, (descriptor, key) => {
247-
if (descriptor.initial && !descriptor._isNoopable) {
248-
descriptor._isNoopable = true
249-
}
250-
})
254+
this.resetValidationNoopable()
251255
this.resetFlags()
252256
this._init = this._getValue(this._el)
253257
}

src/validations/checkbox.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default class CheckboxValidation extends BaseValidation {
9090
}
9191

9292
reset () {
93+
this.resetValidationNoopable()
9394
this.resetFlags()
9495
each(this._inits, (item, index) => {
9596
item.init = item.el.checked

src/validations/radio.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default class RadioValidation extends BaseValidation {
6666
}
6767

6868
reset () {
69+
this.resetValidationNoopable()
6970
this.resetFlags()
7071
each(this._inits, (item, index) => {
7172
item.init = item.el.checked

src/validations/select.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ export default class SelectValidation extends BaseValidation {
5151
this._unwatch && this._unwatch()
5252
}
5353

54-
reset () {
55-
this.resetFlags()
56-
}
57-
5854
_getValue (el) {
5955
let ret = []
6056

src/validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export default class Validator {
5454
delete vm['$setValidationErrors']
5555
vm.$validate = null
5656
delete vm['$validate']
57-
vm.$validatorReset = null
58-
delete vm['$validatorReset']
57+
vm.$resetValidation = null
58+
delete vm['$resetValidation']
5959
vm._validatorMaps[this.name] = null
6060
delete vm._validatorMaps[this.name]
6161
vm[this.name] = null

0 commit comments

Comments
 (0)