1- // 简单示例
2- let simple = { } ;
1+ // validateForm
2+ let validateForm = { } ;
3+
4+ let validateFormFields = [
5+ {
6+ label : '姓名' ,
7+ type : 'Input' ,
8+ model : 'name' ,
9+ placehold : '请输入姓名' ,
10+ rules : [
11+ {
12+ type : 'string' ,
13+ required : true ,
14+ pattern : / ^ [ \u4e00 - \u9fa5 ] + $ / ,
15+ message : '请输入中文姓名'
16+ }
17+ ]
18+ } ,
19+ {
20+ label : '城市' ,
21+ type : 'Select' ,
22+ model : 'city' ,
23+ placehold : '请选择' ,
24+ options : [
25+ { label : '北京' , 'value' : 'Beijing' } ,
26+ { label : '上海' , 'value' : 'Shanghai' } ,
27+ { label : '广州' , 'value' : 'Guangzhou' } ,
28+ { label : '深圳' , 'value' : 'Shenzhen' }
29+ ] ,
30+ required : true
31+ } ,
32+ {
33+ label : '生日' ,
34+ type : 'DatePicker' ,
35+ model : 'birthday' ,
36+ required : true
37+ } ,
38+ {
39+ label : '性别' ,
40+ type : 'Radio' ,
41+ model : 'gender' ,
42+ options : [
43+ { label : '男' , 'value' : 'M' } ,
44+ { label : '女' , 'value' : 'F' }
45+ ] ,
46+ required : true
47+ } ,
48+ {
49+ label : '水果' ,
50+ type : 'Checkbox' ,
51+ model : 'fruit' ,
52+ options : [
53+ { label : '西瓜' , 'value' : '1' } ,
54+ { label : '苹果' , 'value' : '2' } ,
55+ { label : '梨子' , 'value' : '3' }
56+ ] ,
57+ required : true
58+ } ,
59+ {
60+ type : 'Submit' ,
61+ subtype : 'primary' ,
62+ text : '提交' ,
63+ inline : true
64+ } ,
65+ {
66+ type : 'Reset' ,
67+ text : '重置' ,
68+ labelWidth : 0 ,
69+ inline : true
70+ }
71+ ] ;
72+
73+ let validateFormModel = {
74+ name : '' ,
75+ city : '' ,
76+ birthday : '' ,
77+ gender : '' ,
78+ fruit : [ ] ,
79+ } ;
80+
81+
82+ validateForm . data = {
83+ fields : validateFormFields ,
84+ model : validateFormModel
85+ } ;
86+
87+ validateForm . code = `
88+ <script>
89+ const fields = ${ JSON . stringify ( validateFormFields , null , 4 ) } ;
90+ const model = ${ JSON . stringify ( validateFormModel , null , 4 ) } ;
91+ export default {
92+ data() {
93+ return {
94+ fields,
95+ model
96+ };
97+ }
98+ };
99+ </script>
100+ <template>
101+ <FormGenerator
102+ :fields="fields"
103+ :model="model"
104+ />
105+ </template>
106+ ` ;
107+
108+
109+ // required
110+ let required = { } ;
3111
4112const fields = [
5113 {
@@ -15,18 +123,20 @@ const model = {
15123 age : 18 ,
16124} ;
17125
18- simple . data = {
126+ required . data = {
19127 fields,
20128 model
21129} ;
22130
23- simple . code = `
131+ required . code = `
24132<script>
133+ const fields = ${ JSON . stringify ( fields , null , 4 ) } ;
134+ const model = ${ JSON . stringify ( model , null , 4 ) } ;
25135export default {
26136 data() {
27137 return {
28- fields: ${ JSON . stringify ( fields ) } ,
29- model: ${ JSON . stringify ( model ) }
138+ fields,
139+ model
30140 };
31141 }
32142 methods: {
@@ -38,14 +148,16 @@ export default {
38148</script>
39149<template>
40150 <FormGenerator
41- :fields="fields"
42- :model="model"
43- />
151+ :fields="fields"
152+ :model="model"
153+ />
44154</template>
45155` ;
46156
47- let params = { } ;
48- const paramsRules = [
157+
158+ // rules
159+ let rules = { } ;
160+ const rulesRules = [
49161 {
50162 type : 'Input' ,
51163 label : '姓名' ,
@@ -60,21 +172,23 @@ const paramsRules = [
60172 }
61173] ;
62174
63- const paramsModel = {
175+ const rulesModel = {
64176 name : '张三' ,
65177 age : 12
66178} ;
67- params . data = {
68- paramsRules ,
69- paramsModel
179+ rules . data = {
180+ rulesRules ,
181+ rulesModel
70182} ;
71- params . code = `
183+ rules . code = `
72184<script>
185+ const fields = ${ JSON . stringify ( rulesRules , null , 4 ) } ;
186+ const model = ${ JSON . stringify ( rulesModel , null , 4 ) } ;
73187export default {
74188 data() {
75189 return {
76- fields: ${ JSON . stringify ( paramsRules ) } ,
77- model: ${ JSON . stringify ( paramsModel ) }
190+ fields,
191+ model
78192 };
79193 }
80194 methods: {
@@ -92,25 +206,32 @@ export default {
92206</template>
93207` ;
94208
95- // 包含某个值
209+
210+ // enumConfig
96211let enumConfig = { } ;
97212const enumFileds = [
98213 {
99- type : 'Input' ,
100- label : '姓名' ,
101- model : 'name' ,
214+ type : 'Radio' ,
215+ subtype : 'button' ,
216+ label : '是否确认' ,
217+ model : 'asure' ,
218+ options : [
219+ { label : '确认' , value : '1' } ,
220+ { label : '放弃' , value : '0' } ,
221+ ] ,
102222 rules : [
103223 {
104224 type : 'enum' ,
105- enum : [ '张三' ] ,
106- message : '姓名必须包含张三'
225+ enum : [ '1' ] ,
226+ required : true ,
227+ message : '必须选择确认'
107228 }
108229 ]
109230 }
110231] ;
111232
112233const enumModels = {
113- name : '张三 ' ,
234+ asure : '1 ' ,
114235} ;
115236
116237
@@ -121,11 +242,13 @@ enumConfig.data = {
121242
122243enumConfig . code = `
123244<script>
245+ const fields = ${ JSON . stringify ( enumFileds , null , 4 ) } ;
246+ const model = ${ JSON . stringify ( enumModels , null , 4 ) } ;
124247export default {
125248 data() {
126249 return {
127- fields: ${ JSON . stringify ( enumFileds ) } ,
128- model: ${ JSON . stringify ( enumModels ) }
250+ fields,
251+ model
129252 };
130253 }
131254 methods: {
@@ -143,6 +266,7 @@ export default {
143266</template>
144267` ;
145268
269+
146270// rules为alidator
147271let validatorConfig = { } ;
148272
@@ -154,7 +278,7 @@ const validatorModels = {
154278
155279const validatorFileds = [
156280 {
157- type : 'Input ' ,
281+ type : 'InputNumber ' ,
158282 label : '年龄' ,
159283 model : 'age' ,
160284 rules : [ {
@@ -180,13 +304,15 @@ export default {
180304 return {
181305 fields: [
182306 {
183- type: 'Input ',
307+ type: 'InputNumber ',
184308 label: '年龄',
185309 model: 'age',
186310 rules: [{
187311 type: 'number',
188312 validator(rule, value) {
189- return value > 20;
313+ validator(rule, value) {
314+ return value > 20;
315+ },
190316 },
191317 message: '年龄大于20岁'
192318 }]
@@ -210,7 +336,7 @@ export default {
210336</template>
211337` ;
212338
213- // rules为alidator
339+ // rules 为 async validator
214340let asyncValidatorConfig = { } ;
215341
216342const asyncValidatorModels = {
@@ -221,7 +347,7 @@ const asyncValidatorModels = {
221347
222348const asyncValidatorFileds = [
223349 {
224- type : 'Input ' ,
350+ type : 'InputNumber ' ,
225351 label : '年龄' ,
226352 model : 'age' ,
227353 rules : [ {
@@ -234,8 +360,8 @@ const asyncValidatorFileds = [
234360 reject ( '年龄大于20岁~' ) ;
235361 }
236362 } ) ;
237- }
238- // message: '年龄大于20岁!'
363+ } ,
364+ message : '年龄大于20岁!'
239365 } ]
240366 }
241367] ;
@@ -253,7 +379,7 @@ export default {
253379 return {
254380 fields: [
255381 {
256- type: 'Input ',
382+ type: 'InputNumber ',
257383 label: '年龄',
258384 model: 'age',
259385 rules: [{
@@ -288,7 +414,7 @@ export default {
288414</template>
289415` ;
290416
291- // rules为alidator
417+ // submit button
292418let submitConfig = { } ;
293419
294420const submitModels = {
@@ -299,7 +425,7 @@ const submitModels = {
299425
300426const submitFileds = [
301427 {
302- type : 'Input ' ,
428+ type : 'InputNumber ' ,
303429 label : '年龄' ,
304430 model : 'age' ,
305431 rules : [ {
@@ -337,12 +463,12 @@ export default {
337463 return {
338464 fields: [
339465 {
340- type: 'Input ',
466+ type: 'InputNumber ',
341467 label: '年龄',
342468 model: 'age',
343469 rules: [{
344470 type: 'number',
345- submit (rule, value) {
471+ asyncValidator (rule, value) {
346472 return new Promise((resolve, reject) => {
347473 if (value > 20) {
348474 resolve();
@@ -379,11 +505,11 @@ export default {
379505</template>
380506` ;
381507
382- // value
383508export default {
384- simple,
509+ validateForm,
510+ required,
385511 enumConfig,
386- params ,
512+ rules ,
387513 validatorConfig,
388514 asyncValidatorConfig,
389515 submitConfig
0 commit comments