-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-validator.js
2602 lines (2186 loc) · 74.9 KB
/
vue-validator.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* vue-validator v2.1.2
* (c) 2016 kazuya kawaguchi
* Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.VueValidator = factory());
}(this, function () { 'use strict';
var babelHelpers = {};
babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
babelHelpers.classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
babelHelpers.createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
babelHelpers.inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
babelHelpers.possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
babelHelpers;
/**
* Utilties
*/
// export default for holding the Vue reference
var exports$1 = {};
/**
* warn
*
* @param {String} msg
* @param {Error} [err]
*
*/
function warn(msg, err) {
if (window.console) {
console.warn('[vue-validator] ' + msg);
if (err) {
console.warn(err.stack);
}
}
}
/**
* empty
*
* @param {Array|Object} target
* @return {Boolean}
*/
function empty(target) {
if (target === null || target === undefined) {
return true;
}
if (Array.isArray(target)) {
if (target.length > 0) {
return false;
}
if (target.length === 0) {
return true;
}
} else if (exports$1.Vue.util.isPlainObject(target)) {
for (var key in target) {
if (exports$1.Vue.util.hasOwn(target, key)) {
return false;
}
}
}
return true;
}
/**
* each
*
* @param {Array|Object} target
* @param {Function} iterator
* @param {Object} [context]
*/
function each(target, iterator, context) {
if (Array.isArray(target)) {
for (var i = 0; i < target.length; i++) {
iterator.call(context || target[i], target[i], i);
}
} else if (exports$1.Vue.util.isPlainObject(target)) {
var hasOwn = exports$1.Vue.util.hasOwn;
for (var key in target) {
if (hasOwn(target, key)) {
iterator.call(context || target[key], target[key], key);
}
}
}
}
/**
* pull
*
* @param {Array} arr
* @param {Object} item
* @return {Object|null}
*/
function pull(arr, item) {
var index = exports$1.Vue.util.indexOf(arr, item);
return ~index ? arr.splice(index, 1) : null;
}
/**
* trigger
*
* @param {Element} el
* @param {String} event
* @param {Object} [args]
*/
function trigger(el, event, args) {
var e = document.createEvent('HTMLEvents');
e.initEvent(event, true, false);
if (args) {
for (var prop in args) {
e[prop] = args[prop];
}
}
// Due to Firefox bug, events fired on disabled
// non-attached form controls can throw errors
try {
el.dispatchEvent(e);
} catch (e) {}
}
/**
* Forgiving check for a promise
*
* @param {Object} p
* @return {Boolean}
*/
function isPromise(p) {
return p && typeof p.then === 'function';
}
/**
* Togging classes
*
* @param {Element} el
* @param {String} key
* @param {Function} fn
*/
function toggleClasses(el, key, fn) {
key = key.trim();
if (key.indexOf(' ') === -1) {
fn(el, key);
return;
}
var keys = key.split(/\s+/);
for (var i = 0, l = keys.length; i < l; i++) {
fn(el, keys[i]);
}
}
/**
* Fundamental validate functions
*/
/**
* required
*
* This function validate whether the value has been filled out.
*
* @param {*} val
* @return {Boolean}
*/
function required(val) {
if (Array.isArray(val)) {
if (val.length !== 0) {
var valid = true;
for (var i = 0, l = val.length; i < l; i++) {
valid = required(val[i]);
if (!valid) {
break;
}
}
return valid;
} else {
return false;
}
} else if (typeof val === 'number' || typeof val === 'function') {
return true;
} else if (typeof val === 'boolean') {
return val;
} else if (typeof val === 'string') {
return val.length > 0;
} else if (val !== null && (typeof val === 'undefined' ? 'undefined' : babelHelpers.typeof(val)) === 'object') {
return Object.keys(val).length > 0;
} else if (val === null || val === undefined) {
return false;
}
}
/**
* pattern
*
* This function validate whether the value matches the regex pattern
*
* @param val
* @param {String} pat
* @return {Boolean}
*/
function pattern(val, pat) {
if (typeof pat !== 'string') {
return false;
}
var match = pat.match(new RegExp('^/(.*?)/([gimy]*)$'));
if (!match) {
return false;
}
return new RegExp(match[1], match[2]).test(val);
}
/**
* minlength
*
* This function validate whether the minimum length.
*
* @param {String|Array} val
* @param {String|Number} min
* @return {Boolean}
*/
function minlength(val, min) {
if (typeof val === 'string') {
return isInteger(min, 10) && val.length >= parseInt(min, 10);
} else if (Array.isArray(val)) {
return val.length >= parseInt(min, 10);
} else {
return false;
}
}
/**
* maxlength
*
* This function validate whether the maximum length.
*
* @param {String|Array} val
* @param {String|Number} max
* @return {Boolean}
*/
function maxlength(val, max) {
if (typeof val === 'string') {
return isInteger(max, 10) && val.length <= parseInt(max, 10);
} else if (Array.isArray(val)) {
return val.length <= parseInt(max, 10);
} else {
return false;
}
}
/**
* min
*
* This function validate whether the minimum value of the numberable value.
*
* @param {*} val
* @param {*} arg minimum
* @return {Boolean}
*/
function min(val, arg) {
return !isNaN(+val) && !isNaN(+arg) && +val >= +arg;
}
/**
* max
*
* This function validate whether the maximum value of the numberable value.
*
* @param {*} val
* @param {*} arg maximum
* @return {Boolean}
*/
function max(val, arg) {
return !isNaN(+val) && !isNaN(+arg) && +val <= +arg;
}
/**
* isInteger
*
* This function check whether the value of the string is integer.
*
* @param {String} val
* @return {Boolean}
* @private
*/
function isInteger(val) {
return (/^(-?[1-9]\d*|0)$/.test(val)
);
}
var validators = Object.freeze({
required: required,
pattern: pattern,
minlength: minlength,
maxlength: maxlength,
min: min,
max: max
});
function Asset (Vue) {
var extend = Vue.util.extend;
// set global validators asset
var assets = Object.create(null);
extend(assets, validators);
Vue.options.validators = assets;
// set option merge strategy
var strats = Vue.config.optionMergeStrategies;
if (strats) {
strats.validators = function (parent, child) {
if (!child) {
return parent;
}
if (!parent) {
return child;
}
var ret = Object.create(null);
extend(ret, parent);
for (var key in child) {
ret[key] = child[key];
}
return ret;
};
}
/**
* Register or retrieve a global validator definition.
*
* @param {String} id
* @param {Function} definition
*/
Vue.validator = function (id, definition) {
if (!definition) {
return Vue.options['validators'][id];
} else {
Vue.options['validators'][id] = definition;
}
};
}
function Override (Vue) {
// override _init
var init = Vue.prototype._init;
Vue.prototype._init = function (options) {
if (!this._validatorMaps) {
this._validatorMaps = Object.create(null);
}
init.call(this, options);
};
// override _destroy
var destroy = Vue.prototype._destroy;
Vue.prototype._destroy = function () {
destroy.apply(this, arguments);
this._validatorMaps = null;
};
}
var VALIDATE_UPDATE = '__vue-validator-validate-update__';
var PRIORITY_VALIDATE = 16;
var PRIORITY_VALIDATE_CLASS = 32;
var REGEX_FILTER = /[^|]\|[^|]/;
var REGEX_VALIDATE_DIRECTIVE = /^v-validate(?:$|:(.*)$)/;
var REGEX_EVENT = /^v-on:|^@/;
var classId = 0; // ID for validation class
function ValidateClass (Vue) {
var vIf = Vue.directive('if');
var FragmentFactory = Vue.FragmentFactory;
var _Vue$util = Vue.util;
var toArray = _Vue$util.toArray;
var replace = _Vue$util.replace;
var createAnchor = _Vue$util.createAnchor;
/**
* `v-validate-class` directive
*/
Vue.directive('validate-class', {
terminal: true,
priority: vIf.priority + PRIORITY_VALIDATE_CLASS,
bind: function bind() {
var _this = this;
var id = String(classId++);
this.setClassIds(this.el, id);
this.vm.$on(VALIDATE_UPDATE, this.cb = function (classIds, validation, results) {
if (classIds.indexOf(id) > -1) {
validation.updateClasses(results, _this.frag.node);
}
});
this.setupFragment();
},
unbind: function unbind() {
this.vm.$off(VALIDATE_UPDATE, this.cb);
this.teardownFragment();
},
setClassIds: function setClassIds(el, id) {
var childNodes = toArray(el.childNodes);
for (var i = 0, l = childNodes.length; i < l; i++) {
var element = childNodes[i];
if (element.nodeType === 1) {
var hasAttrs = element.hasAttributes();
var attrs = hasAttrs && toArray(element.attributes);
for (var k = 0, _l = attrs.length; k < _l; k++) {
var attr = attrs[k];
if (attr.name.match(REGEX_VALIDATE_DIRECTIVE)) {
var existingId = element.getAttribute(VALIDATE_UPDATE);
var value = existingId ? existingId + ',' + id : id;
element.setAttribute(VALIDATE_UPDATE, value);
}
}
}
if (element.hasChildNodes()) {
this.setClassIds(element, id);
}
}
},
setupFragment: function setupFragment() {
this.anchor = createAnchor('v-validate-class');
replace(this.el, this.anchor);
this.factory = new FragmentFactory(this.vm, this.el);
this.frag = this.factory.create(this._host, this._scope, this._frag);
this.frag.before(this.anchor);
},
teardownFragment: function teardownFragment() {
if (this.frag) {
this.frag.remove();
this.frag = null;
this.factory = null;
}
replace(this.anchor, this.el);
this.anchor = null;
}
});
}
function Validate (Vue) {
var vIf = Vue.directive('if');
var FragmentFactory = Vue.FragmentFactory;
var parseDirective = Vue.parsers.directive.parseDirective;
var _Vue$util = Vue.util;
var inBrowser = _Vue$util.inBrowser;
var bind = _Vue$util.bind;
var on = _Vue$util.on;
var off = _Vue$util.off;
var createAnchor = _Vue$util.createAnchor;
var replace = _Vue$util.replace;
var camelize = _Vue$util.camelize;
var isPlainObject = _Vue$util.isPlainObject;
// Test for IE10/11 textarea placeholder clone bug
function checkTextareaCloneBug() {
if (inBrowser) {
var t = document.createElement('textarea');
t.placeholder = 't';
return t.cloneNode(true).value === 't';
} else {
return false;
}
}
var hasTextareaCloneBug = checkTextareaCloneBug();
/**
* `v-validate` directive
*/
Vue.directive('validate', {
terminal: true,
priority: vIf.priority + PRIORITY_VALIDATE,
params: ['group', 'field', 'detect-blur', 'detect-change', 'initial', 'classes'],
paramWatchers: {
detectBlur: function detectBlur(val, old) {
if (this._invalid) {
return;
}
this.validation.detectBlur = this.isDetectBlur(val);
this.validator.validate(this.field);
},
detectChange: function detectChange(val, old) {
if (this._invalid) {
return;
}
this.validation.detectChange = this.isDetectChange(val);
this.validator.validate(this.field);
}
},
bind: function bind() {
var el = this.el;
if ('development' !== 'production' && el.__vue__) {
warn('v-validate="' + this.expression + '" cannot be used on an instance root element.');
this._invalid = true;
return;
}
if ('development' !== 'production' && (el.hasAttribute('v-if') || el.hasAttribute('v-for'))) {
warn('v-validate cannot be used `v-if` or `v-for` build-in terminal directive ' + 'on an element. these is wrapped with `<template>` or other tags: ' + '(e.g. <validator name="validator">' + '<template v-if="hidden">' + '<input type="text" v-validate:field1="[\'required\']">' + '</template>' + '</validator>).');
this._invalid = true;
return;
}
if ('development' !== 'production' && !(this.arg || this.params.field)) {
warn('you need specify field name for v-validate directive.');
this._invalid = true;
return;
}
var validatorName = this.vm.$options._validator;
if ('development' !== 'production' && !validatorName) {
warn('you need to wrap the elements to be validated in a <validator> element: ' + '(e.g. <validator name="validator">' + '<input type="text" v-validate:field1="[\'required\']">' + '</validator>).');
this._invalid = true;
return;
}
var raw = el.getAttribute('v-model');
var _parseModelRaw = this.parseModelRaw(raw);
var model = _parseModelRaw.model;
var filters = _parseModelRaw.filters;
this.model = model;
this.setupFragment();
this.setupValidate(validatorName, model, filters);
this.listen();
},
update: function update(value, old) {
if (!value || this._invalid) {
return;
}
if (isPlainObject(value)) {
this.handleObject(value);
} else if (Array.isArray(value)) {
this.handleArray(value);
}
var options = { field: this.field, noopable: this._initialNoopValidation };
if (this.frag) {
options.el = this.frag.node;
}
this.validator.validate(options);
if (this._initialNoopValidation) {
this._initialNoopValidation = null;
}
},
unbind: function unbind() {
if (this._invalid) {
return;
}
this.unlisten();
this.teardownValidate();
this.teardownFragment();
this.model = null;
},
parseModelRaw: function parseModelRaw(raw) {
if (REGEX_FILTER.test(raw)) {
var parsed = parseDirective(raw);
return { model: parsed.expression, filters: parsed.filters };
} else {
return { model: raw };
}
},
setupValidate: function setupValidate(name, model, filters) {
var params = this.params;
var validator = this.validator = this.vm._validatorMaps[name];
this.field = camelize(this.arg ? this.arg : params.field);
this.validation = validator.manageValidation(this.field, model, this.vm, this.frag.node, this._scope, filters, params.initial, this.isDetectBlur(params.detectBlur), this.isDetectChange(params.detectChange));
isPlainObject(params.classes) && this.validation.setValidationClasses(params.classes);
params.group && validator.addGroupValidation(params.group, this.field);
this._initialNoopValidation = this.isInitialNoopValidation(params.initial);
},
listen: function listen() {
var model = this.model;
var validation = this.validation;
var el = this.frag.node;
this.onBlur = bind(validation.listener, validation);
on(el, 'blur', this.onBlur);
if ((el.type === 'radio' || el.tagName === 'SELECT') && !model) {
this.onChange = bind(validation.listener, validation);
on(el, 'change', this.onChange);
} else if (el.type === 'checkbox') {
if (!model) {
this.onChange = bind(validation.listener, validation);
on(el, 'change', this.onChange);
} else {
this.onClick = bind(validation.listener, validation);
on(el, 'click', this.onClick);
}
} else {
if (!model) {
this.onInput = bind(validation.listener, validation);
on(el, 'input', this.onInput);
}
}
},
unlisten: function unlisten() {
var el = this.frag.node;
if (this.onInput) {
off(el, 'input', this.onInput);
this.onInput = null;
}
if (this.onClick) {
off(el, 'click', this.onClick);
this.onClick = null;
}
if (this.onChange) {
off(el, 'change', this.onChange);
this.onChange = null;
}
if (this.onBlur) {
off(el, 'blur', this.onBlur);
this.onBlur = null;
}
},
teardownValidate: function teardownValidate() {
if (this.validator && this.validation) {
var el = this.frag.node;
this.params.group && this.validator.removeGroupValidation(this.params.group, this.field);
this.validator.unmanageValidation(this.field, el);
this.validator = null;
this.validation = null;
this.field = null;
}
},
setupFragment: function setupFragment() {
this.anchor = createAnchor('v-validate');
replace(this.el, this.anchor);
this.factory = new FragmentFactory(this.vm, this.shimNode(this.el));
this.frag = this.factory.create(this._host, this._scope, this._frag);
this.frag.before(this.anchor);
},
teardownFragment: function teardownFragment() {
if (this.frag) {
this.frag.remove();
this.frag = null;
this.factory = null;
}
replace(this.anchor, this.el);
this.anchor = null;
},
handleArray: function handleArray(value) {
var _this = this;
each(value, function (val) {
_this.validation.setValidation(val);
});
},
handleObject: function handleObject(value) {
var _this2 = this;
each(value, function (val, key) {
if (isPlainObject(val)) {
if ('rule' in val) {
var msg = 'message' in val ? val.message : null;
var initial = 'initial' in val ? val.initial : null;
_this2.validation.setValidation(key, val.rule, msg, initial);
}
} else {
_this2.validation.setValidation(key, val);
}
});
},
isDetectBlur: function isDetectBlur(detectBlur) {
return detectBlur === undefined || detectBlur === 'on' || detectBlur === true;
},
isDetectChange: function isDetectChange(detectChange) {
return detectChange === undefined || detectChange === 'on' || detectChange === true;
},
isInitialNoopValidation: function isInitialNoopValidation(initial) {
return initial === 'off' || initial === false;
},
shimNode: function shimNode(node) {
var ret = node;
if (hasTextareaCloneBug) {
if (node.tagName === 'TEXTAREA') {
ret = node.cloneNode(true);
ret.value = node.value;
var i = ret.childNodes.length;
while (i--) {
ret.removeChild(ret.childNodes[i]);
}
}
}
return ret;
}
});
}
/**
* BaseValidation class
*/
var BaseValidation = function () {
function BaseValidation(field, model, vm, el, scope, validator, filters, detectBlur, detectChange) {
babelHelpers.classCallCheck(this, BaseValidation);
this.field = field;
this.touched = false;
this.dirty = false;
this.modified = false;
this._modified = false;
this._model = model;
this._filters = filters;
this._validator = validator;
this._vm = vm;
this._el = el;
this._forScope = scope;
this._init = this._getValue(el);
this._validators = {};
this._detectBlur = detectBlur;
this._detectChange = detectChange;
this._classes = {};
}
BaseValidation.prototype.manageElement = function manageElement(el, initial) {
var _this = this;
var scope = this._getScope();
var model = this._model;
this._initial = initial;
var classIds = el.getAttribute(VALIDATE_UPDATE);
if (classIds) {
el.removeAttribute(VALIDATE_UPDATE);
this._classIds = classIds.split(',');
}
if (model) {
el.value = this._evalModel(model, this._filters);
this._unwatch = scope.$watch(model, function (val, old) {
if (val !== old) {
if (_this.guardValidate(el, 'input')) {
return;
}
_this.handleValidate(el, { noopable: _this._initial });
if (_this._initial) {
_this._initial = null;
}
}
}, { deep: true });
}
};
BaseValidation.prototype.unmanageElement = function unmanageElement(el) {
this._unwatch && this._unwatch();
};
BaseValidation.prototype.setValidation = function setValidation(name, arg, msg, initial) {
var validator = this._validators[name];
if (!validator) {
validator = this._validators[name] = {};
validator.name = name;
}
validator.arg = arg;
if (msg) {
validator.msg = msg;
}
if (initial) {
validator.initial = initial;
validator._isNoopable = true;
}
};
BaseValidation.prototype.setValidationClasses = function setValidationClasses(classes) {
var _this2 = this;
each(classes, function (value, key) {
_this2._classes[key] = value;
});
};
BaseValidation.prototype.willUpdateFlags = function willUpdateFlags() {
var touched = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];
touched && this.willUpdateTouched(this._el, 'blur');
this.willUpdateDirty(this._el);
this.willUpdateModified(this._el);
};
BaseValidation.prototype.willUpdateTouched = function willUpdateTouched(el, type) {
if (type && type === 'blur') {
this.touched = true;
this._fireEvent(el, 'touched');
}
};
BaseValidation.prototype.willUpdateDirty = function willUpdateDirty(el) {
if (!this.dirty && this._checkModified(el)) {
this.dirty = true;
this._fireEvent(el, 'dirty');
}
};
BaseValidation.prototype.willUpdateModified = function willUpdateModified(el) {
this.modified = this._checkModified(el);
if (this._modified !== this.modified) {
this._fireEvent(el, 'modified', { modified: this.modified });
this._modified = this.modified;
}
};
BaseValidation.prototype.listener = function listener(e) {
if (this.guardValidate(e.target, e.type)) {
return;
}
this.handleValidate(e.target, { type: e.type });
};
BaseValidation.prototype.handleValidate = function handleValidate(el) {
var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var _ref$type = _ref.type;
var type = _ref$type === undefined ? null : _ref$type;
var _ref$noopable = _ref.noopable;
var noopable = _ref$noopable === undefined ? false : _ref$noopable;
this.willUpdateTouched(el, type);
this.willUpdateDirty(el);
this.willUpdateModified(el);
this._validator.validate({ field: this.field, el: el, noopable: noopable });
};
BaseValidation.prototype.validate = function validate(cb) {
var _this3 = this;
var noopable = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
var el = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var _ = exports$1.Vue.util;
var results = {};
var errors = [];
var valid = true;
this._runValidators(function (descriptor, name, done) {
var asset = _this3._resolveValidator(name);
var validator = null;
var msg = null;
if (_.isPlainObject(asset)) {
if (asset.check && typeof asset.check === 'function') {
validator = asset.check;
}
if (asset.message) {
msg = asset.message;
}
} else if (typeof asset === 'function') {
validator = asset;
}
if (descriptor.msg) {
msg = descriptor.msg;
}
if (noopable) {
results[name] = false;
return done();
}
if (descriptor._isNoopable) {
results[name] = false;
descriptor._isNoopable = null;
return done();
}
if (validator) {
var value = _this3._getValue(_this3._el);
_this3._invokeValidator(_this3._vm, validator, value, descriptor.arg, function (ret, err) {
if (!ret) {
valid = false;
if (err) {
// async error message
errors.push({ validator: name, message: err });
results[name] = err;
} else if (msg) {
var error = { validator: name };
error.message = typeof msg === 'function' ? msg.call(_this3._vm, _this3.field, descriptor.arg) : msg;
errors.push(error);
results[name] = error.message;
} else {
results[name] = !ret;
}
} else {
results[name] = !ret;
}
done();
});
} else {
done();