-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlValidator.js
55 lines (53 loc) · 1.25 KB
/
ControlValidator.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
import Rules from './rules'
import { isEqual } from 'lodash'
export default {
name: 'control-validator',
props: {
value: {
required: true
},
text: String,
rules: {
type: [Object, String]
},
delay: {
type: Number,
default: 300
}
},
created () {
this._rules = new Rules(this.rules, {text: this.text})
this.$emit('update:isValid', !!this._rules.isValid(this.value))
},
watch: {
value (value, old) {
if (this.delay > 0) {
if (this._timer) {
clearTimeout(this._timer)
}
this._timer = setTimeout(() => this.check(value), this.delay)
} else {
this.check(value)
}
}
},
methods: {
check (value) {
// if (value === undefined) {
// value = this.value
// }
let validation = this._rules.check(value)
// check is same arrays
if (!(isEqual(validation, this.lastValidation))) {
this.lastValidation = validation || null
this.child.componentInstance.$emit('validation', validation)
this.$emit('update:isValid', !validation)
}
return validation
}
},
render (h) {
this.child = this.$options._renderChildren[0]
return this.$options._renderChildren[0]
}
}