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
| <script lang="ts" setup>
| import { Page } from '@vben/common-ui';
|
| import { Button, Card, message } from 'ant-design-vue';
|
| import { useVbenForm, z } from '#/adapter/form';
|
| const [Form, formApi] = useVbenForm({
| // 所有表单项共用,可单独在表单内覆盖
| commonConfig: {
| // 所有表单项
| componentProps: {
| class: 'w-full',
| },
| },
| // 提交函数
| handleSubmit: onSubmit,
| // 垂直布局,label和input在不同行,值为vertical
| // 水平布局,label和input在同一行
| layout: 'horizontal',
| schema: [
| {
| // 组件需要在 #/adapter.ts内注册,并加上类型
| component: 'Input',
| // 对应组件的参数
| componentProps: {
| placeholder: '请输入',
| },
| // 字段名
| fieldName: 'field1',
| // 界面显示的label
| label: '字段1',
| rules: 'required',
| },
| {
| component: 'Input',
| componentProps: {
| placeholder: '请输入',
| },
| defaultValue: '默认值',
| fieldName: 'field2',
| label: '默认值(必填)',
| rules: 'required',
| },
| {
| component: 'Input',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'field3',
| label: '默认值(非必填)',
| rules: z.string().default('默认值').optional(),
| },
| {
| component: 'Input',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'field31',
| label: '自定义信息',
| rules: z.string().min(1, { message: '最少输入1个字符' }),
| },
| {
| component: 'Input',
| // 对应组件的参数
| componentProps: {
| placeholder: '请输入',
| },
| // 字段名
| fieldName: 'field4',
| // 界面显示的label
| label: '邮箱',
| rules: z.string().email('请输入正确的邮箱'),
| },
| {
| component: 'InputNumber',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'number',
| label: '数字',
| rules: 'required',
| },
| {
| component: 'Select',
| componentProps: {
| allowClear: true,
| filterOption: true,
| options: [
| {
| label: '选项1',
| value: '1',
| },
| {
| label: '选项2',
| value: '2',
| },
| ],
| placeholder: '请选择',
| showSearch: true,
| },
| defaultValue: undefined,
| fieldName: 'options',
| label: '下拉选',
| rules: 'selectRequired',
| },
| {
| component: 'RadioGroup',
| componentProps: {
| options: [
| {
| label: '选项1',
| value: '1',
| },
| {
| label: '选项2',
| value: '2',
| },
| ],
| },
| fieldName: 'radioGroup',
| label: '单选组',
| rules: 'selectRequired',
| },
| {
| component: 'CheckboxGroup',
| componentProps: {
| name: 'cname',
| options: [
| {
| label: '选项1',
| value: '1',
| },
| {
| label: '选项2',
| value: '2',
| },
| ],
| },
| fieldName: 'checkboxGroup',
| label: '多选组',
| rules: 'selectRequired',
| },
| {
| component: 'Checkbox',
| fieldName: 'checkbox',
| label: '',
| renderComponentContent: () => {
| return {
| default: () => ['我已阅读并同意'],
| };
| },
| rules: z.boolean().refine((value) => value, {
| message: '请勾选',
| }),
| },
| {
| component: 'DatePicker',
| defaultValue: undefined,
| fieldName: 'datePicker',
| label: '日期选择框',
| rules: 'selectRequired',
| },
| {
| component: 'RangePicker',
| defaultValue: undefined,
| fieldName: 'rangePicker',
| label: '区间选择框',
| rules: 'selectRequired',
| },
| {
| component: 'InputPassword',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'password',
| label: '密码',
| rules: 'required',
| },
| {
| component: 'Input',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'input-blur',
| formFieldProps: {
| validateOnChange: false,
| validateOnModelUpdate: false,
| },
| help: 'blur时才会触发校验',
| label: 'blur触发',
| rules: 'required',
| },
| {
| component: 'Input',
| componentProps: {
| placeholder: '请输入',
| },
| fieldName: 'input-async',
| label: '异步校验',
| rules: z
| .string()
| .min(3, '用户名至少需要3个字符')
| .refine(
| async (username) => {
| // 假设这是一个异步函数,模拟检查用户名是否已存在
| const checkUsernameExists = async (
| username: string,
| ): Promise<boolean> => {
| await new Promise((resolve) => setTimeout(resolve, 1000));
| return username === 'existingUser';
| };
| const exists = await checkUsernameExists(username);
| return !exists;
| },
| {
| message: '用户名已存在',
| },
| ),
| },
| ],
| // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
| wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
| });
|
| function onSubmit(values: Record<string, any>) {
| message.success({
| content: `form values: ${JSON.stringify(values)}`,
| });
| }
| </script>
|
| <template>
| <Page description="表单校验示例" title="表单组件">
| <Card title="基础组件校验示例">
| <template #extra>
| <Button @click="() => formApi.validate()">校验表单</Button>
| <Button class="mx-2" @click="() => formApi.resetValidate()">
| 清空校验信息
| </Button>
| </template>
| <Form />
| </Card>
| </Page>
| </template>
|
|