forked from kazupon/vue-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (106 loc) · 4.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Import(s)
*/
var validates = require('./lib/validates')
var _ = require('./lib/util')
/**
* Export(s)
*/
module.exports = install
/**
* Install plugin
*/
function install (Vue, options) {
options = options || {}
var componentName = options.component = options.component || '$validator'
var directiveName = options.directive = options.directive || 'validate'
var path = Vue.parsers.path
function getVal (obj, keypath) {
var ret = null
try {
ret = path.get(obj, keypath)
} catch (e) { }
return ret
}
Vue.directive(directiveName, {
priority: 1024,
bind: function () {
var vm = this.vm
var el = this.el
var $validator = vm[componentName]
var keypath = this._keypath = this._parseModelAttribute(el.getAttribute(Vue.config.prefix + 'model'))
var validator = this.arg ? this.arg : this.expression
var arg = this.arg ? this.expression : null
var customs = (vm.$options.validator && vm.$options.validator.validates) || {}
if (!this._checkDirective(validator, validates, customs)) {
_.warn('specified invalid v-validate directive !! please check v-validator directive !!')
this._ignore = true
return
}
if (!$validator) {
vm[componentName] = $validator = vm.$addChild({
validator: vm.$options.validator
}, Vue.extend(require('./lib/validator')))
}
var validation = $validator._getValidationNamespace('validation')
var init = el.getAttribute('value') || vm.$get(keypath)
var readyEvent = el.getAttribute('wait-for')
if (readyEvent && !$validator._isRegistedReadyEvent(keypath)) {
$validator._addReadyEvents(keypath, this._checkParam('wait-for'))
}
this._setupValidator($validator, keypath, validation, validator, arg, init)
},
update: function (val, old) {
if (this._ignore) { return }
var vm = this.vm
var keypath = this._keypath
var validator = this.arg ? this.arg : this.expression
var $validator = vm[componentName]
$validator._changeValidator(keypath, validator, val)
if (!$validator._isRegistedReadyEvent(keypath)) { // normal
$validator._updateDirtyProperty(keypath, $validator.$get(keypath))
$validator._doValidate(keypath, validator, $validator.$get(keypath))
} else { // wait-for
vm.$once($validator._getReadyEvents(keypath), function (val) {
$validator._setInitialValue(keypath, val)
vm.$set(keypath, val)
$validator._updateDirtyProperty(keypath, $validator.$get(keypath))
$validator._doValidate(keypath, validator, $validator.$get(keypath))
})
}
},
unbind: function () {
if (this._ignore) { return }
var vm = this.vm
var keypath = this._keypath
var validator = this.arg ? this.arg : this.expression
var $validator = vm[componentName]
this._teardownValidator(vm, $validator, keypath, validator)
},
_parseModelAttribute: function (attr) {
var res = Vue.parsers.directive.parse(attr)
return res[0].arg ? res[0].arg : res[0].expression
},
_checkDirective: function (validator, validates, customs) {
var items = Object.keys(validates).concat(Object.keys(customs))
return items.some(function (item) {
return item === validator
})
},
_setupValidator: function ($validator, keypath, validation, validator, arg, init) {
var vm = this.vm
if (!getVal($validator[validation], keypath)) {
$validator._defineModelValidationScope(keypath)
$validator._setInitialValue(keypath, init)
}
if (!getVal($validator[validation], [keypath, validator].join('.'))) {
$validator._defineValidatorToValidationScope(keypath, validator)
$validator._addValidator(keypath, validator, getVal(vm, arg) || arg)
}
},
_teardownValidator: function (vm, $validator, keypath, validator) {
$validator._undefineValidatorToValidationScope(keypath, validator)
$validator._undefineModelValidationScope(keypath, validator)
}
})
}