-
Notifications
You must be signed in to change notification settings - Fork 0
/
va-validation-behavior.html
184 lines (152 loc) · 4.25 KB
/
va-validation-behavior.html
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<script>
VA.ValidationBehavior = {
observers: [
'_validateByPath(data.*)'
],
properties: {
data: {
type: Object
},
fields: {
type: Object,
readOnly: true
},
messages: {
type: Object,
readOnly: true
},
valid: {
notify: true,
type: Boolean,
value: false
},
validation: {
notify: true,
type: Object,
value: function () {
return {};
}
},
errorMessages: {
notify: true,
type: Object,
value: function () {
return {};
}
}
},
ready: function () {
if (this.fields) {
for (var field in this.fields) {
if (this.fields[field].empty === true) {
this.validation[field] = true;
} else {
this.validation[field] = false;
}
this.errorMessages[field] = null;
}
}
},
reset: function () {
this.valid = false;
},
validate: function (name, value, notRecheck) {
var result = true;
var field = this.fields[name];
if (!field) {
throw new Error("There is no rule for '" + field + "'");
}
if (!this.validation[name]) {
this._unsetErrorMessage(name);
}
var target = this.data[name];
if (field.required) {
result = target !== undefined && target !== null && target !== '';
if (!result) {
this._setErrorMessage(name, 'required');
}
}
if (result && field.empty === false) {
result = target !== '';
}
if (result && field.rules) {
for (var i in field.rules) {
var rule = field.rules[i];
result = this._validateForRule(rule, target);
if (!result) {
this._setErrorMessage(name, rule);
break;
}
}
}
if (field.recheck && !notRecheck) {
this.validate(field.recheck, this.data[field.recheck], true);
}
this._setValidation(name, result);
},
forceInvalid: function (name, ruleForMsg) {
if (this.validation[name]) {
this._setValidation(name, false);
if (!ruleForMsg) {
this._unsetErrorMessage(name);
} else {
this._setErrorMessage(name, ruleForMsg);
}
}
},
forceInvalidAll: function () {
for (var name in this.validation) {
if (this.validation[name]) {
this._setValidation(name, false);
this._unsetErrorMessage(name);
}
}
},
_validateForRule: function (rule, target) {
if (this[rule] && typeof this[rule] === 'function') {
return this[rule](target);
}
console.warn('Trying to validate but no rule is found. [rule]:' + rule);
return true;
},
_setValidation: function (name, value) {
this.set(['validation', name], value);
this.valid = this._validAll();
},
_setErrorMessage: function (name, rule) {
if (this.messages && this.messages[name] && this.messages[name][rule]) {
this.set(['errorMessages', name], this.messages[name][rule]);
}
},
_unsetErrorMessage: function (name) {
this.set(['errorMessages', name], '');
},
_validateByPath: function (data) {
var matched;
if (data.path === 'data') {
Object.getOwnPropertyNames(this.fields).forEach(function (key) {
var value = this.get(['data', key]);
if (value !== undefined) {
this.validate(key, value);
}
}.bind(this));
} else {
matched = data.path.match('^data\.(.*)');
var key = matched[1];
var value = this.get(matched[0]);
if (this.fields.hasOwnProperty(key) && value !== undefined) {
this.validate(key, value);
}
}
},
_validAll: function () {
var keys = Object.getOwnPropertyNames(this.validation);
for(var key in this.validation) {
if (!this.validation[key]) {
return false;
}
}
return true;
}
};
</script>