5656<script >
5757import {Input , Tag } from ' iview' ;
5858import {getValue } from ' ../utils/processValue' ;
59+ import schema from ' async-validator' ;
5960
6061export default {
6162 inject: [' form' ],
@@ -97,17 +98,63 @@ export default {
9798 this .handleAdd (e);
9899 }
99100 },
101+ // 通过field.inputRules校验
102+ validator () {
103+ let formItem = this .$parent ;
104+
105+ if (typeof this .field .inputRules === ' object' ) {
106+ let descriptor = {};
107+ descriptor[this .field .model ] = this .field .inputRules ;
108+
109+ let validator = new schema (descriptor);
110+ let model = {};
111+
112+ model[this .field .model ] = this .value ;
113+
114+ validator .validate (model, errors => {
115+ if (errors) {
116+ this .validateMessage = errors ? errors[0 ].message : ' ' ;
117+
118+ formItem .validateState = ' error' ;
119+ formItem .validateMessage = this .validateMessage ;
120+ return ;
121+ } else {
122+ formItem .validateState = ' success' ;
123+ this .validateMessage = ' ' ;
124+ }
125+ });
126+ } else {
127+ formItem .validateState = ' success' ;
128+ this .validateMessage = ' ' ;
129+ }
130+ },
100131 handleAdd (e ) {
101132 if (! this .value ) {
102133 this .$Message .warning (' 请输入数据后添加' );
103134 return ;
104135 }
105- if (this .list .indexOf (this .value ) === - 1 ) {
106- this .field .succMessage && this .$Message .success (this .field .succMessage );
107- this .list .push (this .value );
108- this .value = ' ' ;
109- this .$emit (' on-change' , this .field .model , this .list , e, this .field );
136+ if (this .list .indexOf (this .value ) !== - 1 ) {
137+ this .$Message .warning (` ${ this .value } 已存在` );
138+ return ;
110139 }
140+ if (this .defaultList .indexOf (this .value ) !== - 1 ) {
141+ this .$Message .warning (` ${ this .value } 已存在` );
142+ return ;
143+ }
144+
145+ if (! this .validateMessage ) {
146+ this .addMember (e);
147+ }
148+ },
149+ addMember (e ) {
150+ let formItem = this .$parent ;
151+ formItem .validateState = ' success' ;
152+
153+ this .field .succMessage && this .$Message .success (this .field .succMessage );
154+ this .list .push (this .value );
155+ this .value = ' ' ;
156+ // this.$set(this.form.model, this.field.model, this.list);
157+ this .$emit (' on-change' , this .field .model , this .list , e, this .field );
111158 },
112159 handelMemberDelete (i , e ) {
113160 this .list .splice (i, 1 );
@@ -116,6 +163,7 @@ export default {
116163 },
117164 handleChange (e ) {
118165 this .value = e .target .value ;
166+ this .validator ();
119167 }
120168 }
121169};
0 commit comments