1
1
/*!
2
- * vue-validator v2.0.0-alpha.4
2
+ * vue-validator v2.0.0-alpha.5
3
3
* (c) 2015 kazuya kawaguchi
4
4
* Released under the MIT License.
5
5
*/
@@ -131,6 +131,7 @@ return /******/ (function(modules) { // webpackBootstrap
131
131
value : true
132
132
} ) ;
133
133
exports . warn = warn ;
134
+ exports . empty = empty ;
134
135
exports . each = each ;
135
136
exports . pull = pull ;
136
137
exports . attr = attr ;
@@ -155,6 +156,36 @@ return /******/ (function(modules) { // webpackBootstrap
155
156
}
156
157
}
157
158
159
+ /**
160
+ * empty
161
+ *
162
+ * @param {Array|Object } target
163
+ * @return {Boolean }
164
+ */
165
+
166
+ function empty ( target ) {
167
+ if ( target === null ) {
168
+ return true ;
169
+ }
170
+
171
+ if ( Array . isArray ( target ) ) {
172
+ if ( target . length > 0 ) {
173
+ return false ;
174
+ }
175
+ if ( target . length === 0 ) {
176
+ return true ;
177
+ }
178
+ } else if ( _exports . Vue . util . isPlainObject ( target ) ) {
179
+ for ( var key in target ) {
180
+ if ( _exports . Vue . util . hasOwn ( target , key ) ) {
181
+ return false ;
182
+ }
183
+ }
184
+ }
185
+
186
+ return true ;
187
+ }
188
+
158
189
/**
159
190
* each
160
191
*
@@ -169,8 +200,9 @@ return /******/ (function(modules) { // webpackBootstrap
169
200
iterator . call ( context || target [ i ] , target [ i ] , i ) ;
170
201
}
171
202
} else if ( _exports . Vue . util . isPlainObject ( target ) ) {
203
+ var hasOwn = _exports . Vue . util . hasOwn ;
172
204
for ( var key in target ) {
173
- if ( key in target ) {
205
+ if ( hasOwn ( target , key ) ) {
174
206
iterator . call ( context || target [ key ] , target [ key ] , key ) ;
175
207
}
176
208
}
@@ -504,7 +536,7 @@ return /******/ (function(modules) { // webpackBootstrap
504
536
( 0 , _util . each ) ( value , function ( val , key ) {
505
537
if ( _ . isPlainObject ( val ) ) {
506
538
if ( 'rule' in val ) {
507
- _this . validation . updateValidate ( key , val . rule ) ;
539
+ _this . validation . updateValidate ( key , val . rule , 'message' in val ? val . message : null ) ;
508
540
}
509
541
} else {
510
542
_this . validation . updateValidate ( key , val ) ;
@@ -587,9 +619,12 @@ return /******/ (function(modules) { // webpackBootstrap
587
619
}
588
620
} , {
589
621
key : 'updateValidate' ,
590
- value : function updateValidate ( name , arg , fn ) {
622
+ value : function updateValidate ( name , arg , msg , fn ) {
591
623
if ( this . validates [ name ] ) {
592
624
this . validates [ name ] . arg = arg ;
625
+ if ( msg ) {
626
+ this . validates [ name ] . msg = msg ;
627
+ }
593
628
if ( fn ) {
594
629
this . validates [ name ] . fn = fn ;
595
630
}
@@ -620,28 +655,37 @@ return /******/ (function(modules) { // webpackBootstrap
620
655
var _this = this ;
621
656
622
657
var extend = _util2 [ 'default' ] . Vue . util . extend ;
623
- var ret = { } ;
658
+ var ret = Object . create ( null ) ;
659
+ var messages = Object . create ( null ) ;
624
660
var valid = true ;
625
661
626
662
( 0 , _util . each ) ( this . validates , function ( descriptor , name ) {
627
663
var res = descriptor . fn ( _this . el . value , descriptor . arg ) ;
628
664
if ( ! res ) {
629
665
valid = false ;
666
+ var msg = descriptor . msg ;
667
+ if ( msg ) {
668
+ messages [ name ] = typeof msg === 'function' ? msg ( ) : msg ;
669
+ }
630
670
}
631
671
ret [ name ] = ! res ;
632
672
} , this ) ;
633
673
634
674
( 0 , _util . trigger ) ( this . el , valid ? 'valid' : 'invalid' ) ;
635
675
636
- extend ( ret , {
676
+ var props = {
637
677
valid : valid ,
638
678
invalid : ! valid ,
639
679
touched : this . touched ,
640
680
untouched : ! this . touched ,
641
681
dirty : this . dirty ,
642
682
pristine : ! this . dirty ,
643
683
modified : this . modified
644
- } ) ;
684
+ } ;
685
+ if ( ! ( 0 , _util . empty ) ( messages ) ) {
686
+ props . messages = messages ;
687
+ }
688
+ extend ( ret , props ) ;
645
689
646
690
return ret ;
647
691
}
@@ -767,17 +811,12 @@ return /******/ (function(modules) { // webpackBootstrap
767
811
_classCallCheck ( this , Validator ) ;
768
812
769
813
this . name = name ;
770
- this . scope = { } ; // TODO: change to Object.create(null)
771
- /*
772
- this.scope = Object.create(null)
773
- this.scope.a = 1
774
- */
775
-
814
+ this . scope = Object . create ( null ) ;
776
815
this . _dir = dir ;
777
816
this . _validations = [ ] ;
778
817
this . _groups = groups ;
779
-
780
818
this . _groupValidations = Object . create ( null ) ;
819
+
781
820
( 0 , _util . each ) ( groups , function ( group ) {
782
821
_this . _groupValidations [ group ] = [ ] ;
783
822
} , this ) ;
@@ -803,7 +842,7 @@ return /******/ (function(modules) { // webpackBootstrap
803
842
} , {
804
843
key : 'removeValidation' ,
805
844
value : function removeValidation ( validation ) {
806
- _util2 [ 'default' ] . Vue . util [ 'delete' ] ( this . scope , validation . model ) ;
845
+ _util2 [ 'default' ] . Vue . util . del ( this . scope , validation . model ) ;
807
846
( 0 , _util . pull ) ( this . _validations , validation ) ;
808
847
}
809
848
} , {
@@ -860,7 +899,8 @@ return /******/ (function(modules) { // webpackBootstrap
860
899
untouched : { fn : this . _defineUntouched , arg : target } ,
861
900
modified : { fn : this . _defineModified , arg : validations } ,
862
901
dirty : { fn : this . _defineDirty , arg : validations } ,
863
- pristine : { fn : this . _definePristine , arg : target }
902
+ pristine : { fn : this . _definePristine , arg : target } ,
903
+ messages : { fn : this . _defineMessages , arg : validations }
864
904
} , function ( descriptor , name ) {
865
905
Object . defineProperty ( target , name , {
866
906
enumerable : true ,
@@ -876,13 +916,14 @@ return /******/ (function(modules) { // webpackBootstrap
876
916
value : function _walkValidations ( validations , property , condition ) {
877
917
var _this5 = this ;
878
918
919
+ var hasOwn = _util2 [ 'default' ] . Vue . util . hasOwn ;
879
920
var ret = condition ;
880
921
881
922
( 0 , _util . each ) ( validations , function ( validation , index ) {
882
923
if ( ret === ! condition ) {
883
924
return ;
884
925
}
885
- if ( Object . prototype . hasOwnProperty . call ( _this5 . scope , validation . model ) ) {
926
+ if ( hasOwn ( _this5 . scope , validation . model ) ) {
886
927
var target = _this5 . scope [ validation . model ] ;
887
928
if ( target && target [ property ] === ! condition ) {
888
929
ret = ! condition ;
@@ -927,6 +968,26 @@ return /******/ (function(modules) { // webpackBootstrap
927
968
value : function _definePristine ( scope ) {
928
969
return ! scope . dirty ;
929
970
}
971
+ } , {
972
+ key : '_defineMessages' ,
973
+ value : function _defineMessages ( validations ) {
974
+ var _this6 = this ;
975
+
976
+ var extend = _util2 [ 'default' ] . Vue . util . extend ;
977
+ var hasOwn = _util2 [ 'default' ] . Vue . util . hasOwn ;
978
+ var ret = Object . create ( null ) ;
979
+
980
+ ( 0 , _util . each ) ( validations , function ( validation , index ) {
981
+ if ( hasOwn ( _this6 . scope , validation . model ) ) {
982
+ var target = _this6 . scope [ validation . model ] ;
983
+ if ( target && ! ( 0 , _util . empty ) ( target [ 'messages' ] ) ) {
984
+ ret [ validation . model ] = extend ( Object . create ( null ) , target [ 'messages' ] ) ;
985
+ }
986
+ }
987
+ } , this ) ;
988
+
989
+ return ( 0 , _util . empty ) ( ret ) ? undefined : ret ;
990
+ }
930
991
} ] ) ;
931
992
932
993
return Validator ;
0 commit comments