1212</template >
1313
1414<script >
15- import AsyncValidator from ' async-validator'
1615
1716export default {
1817 name: ' VueForm' ,
@@ -28,10 +27,6 @@ export default {
2827 type: Object ,
2928 default : () => {}
3029 },
31- rules: {
32- type: Object ,
33- default : () => {}
34- },
3530 labelPosition: {
3631 type: String ,
3732 default: ' right'
@@ -40,68 +35,6 @@ export default {
4035 type: String ,
4136 default: ' 100px'
4237 }
43- },
44-
45- data () {
46- return {
47- validator: this .rules ? new AsyncValidator (this .rules ) : undefined
48- }
49- },
50-
51- methods: {
52- validate () {
53- return new Promise ((resolve , reject ) => {
54- console .warn (this .validator )
55- this .validator .validate (this .model , (err , f ) => {
56- this .$children .forEach (child => {
57- if (! this .isChildFormItem (child)) return
58-
59- this .setChildOptions (child, ' ' , true , true )
60- })
61-
62- if (! err) return resolve (' Form is valid' )
63-
64- Object .keys (this .model ).forEach (item => {
65- err .forEach (err => {
66- if (err .field === item) {
67- this .$children .forEach (child => {
68- if (! this .isChildFormItem (child)) return
69-
70- if (child .field === item) {
71- this .setChildOptions (child, err .message , false , true )
72- }
73- })
74- }
75- })
76- return reject (new Error (' Form is not valid' ))
77- })
78- })
79- })
80- },
81- resetValidation () {
82- this .$children .forEach (child => {
83- if (! this .isChildFormItem (child)) return
84-
85- this .setChildOptions (child, ' ' , true , false )
86- })
87- },
88- resetFieldValidation (field ) {
89- this .$children .forEach (child => {
90- if (! this .isChildFormItem (child)) return
91-
92- if (child .field === field) {
93- this .setChildOptions (child, ' ' , true , false )
94- }
95- })
96- },
97- isChildFormItem (child ) {
98- return child .$options .name === ' VueFormItem'
99- },
100- setChildOptions (child , message , valid , state ) {
101- child .validateMessage = message
102- child .isValid = valid
103- child .validateState = state
104- }
10538 }
10639}
10740 </script >
0 commit comments