|
1
|
<template>
|
vben
authored
|
2
|
<PageWrapper title="表单校验示例">
|
|
3
|
<div class="mb-4">
|
vben
authored
|
4
5
6
7
|
<a-button @click="validateForm" class="mr-2"> 手动校验表单 </a-button>
<a-button @click="resetValidate" class="mr-2"> 清空校验信息 </a-button>
<a-button @click="getFormValues" class="mr-2"> 获取表单值 </a-button>
<a-button @click="setFormValues" class="mr-2"> 设置表单值 </a-button>
|
Vben
authored
|
8
|
<a-button @click="resetFields" class="mr-2"> 重置 </a-button>
|
|
9
10
11
12
|
</div>
<CollapseContainer title="表单校验">
<BasicForm @register="register" @submit="handleSubmit" />
</CollapseContainer>
|
vben
authored
|
13
|
</PageWrapper>
|
|
14
15
16
17
|
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
|
|
18
|
import { CollapseContainer } from '/@/components/Container';
|
|
19
|
import { useMessage } from '/@/hooks/web/useMessage';
|
vben
authored
|
20
|
import { PageWrapper } from '/@/components/Page';
|
|
21
|
import { isAccountExist } from '/@/api/demo/system';
|
vben
authored
|
22
|
|
|
23
24
25
26
27
28
29
30
|
const schemas: FormSchema[] = [
{
field: 'field1',
component: 'Input',
label: '字段1',
colProps: {
span: 8,
},
|
vben
authored
|
31
|
required: true,
|
|
32
33
34
35
36
37
38
39
|
},
{
field: 'field2',
component: 'Input',
label: '字段2',
colProps: {
span: 8,
},
|
vben
authored
|
40
|
required: true,
|
|
41
42
|
},
{
|
Vben
authored
|
43
44
45
46
47
48
49
50
|
field: 'id',
label: 'id',
required: true,
defaultValue: 0,
component: 'InputNumber',
show: false,
},
{
|
|
51
52
53
54
55
56
|
field: 'field3',
component: 'DatePicker',
label: '字段3',
colProps: {
span: 8,
},
|
vben
authored
|
57
|
required: true,
|
|
58
59
|
},
{
|
Vben
authored
|
60
61
62
63
64
65
66
67
68
69
70
71
|
field: 'field33',
component: 'DatePicker',
label: '字段33',
colProps: {
span: 8,
},
componentProps: {
valueFormat: 'YYYY-MM-DD',
},
rules: [{ required: true, type: 'string' }],
},
{
|
Vben
authored
|
72
73
74
75
76
77
78
79
80
|
field: 'field44',
component: 'InputCountDown',
label: '验证码',
colProps: {
span: 8,
},
required: true,
},
{
|
|
81
82
83
84
85
86
87
|
field: 'field4',
component: 'Select',
label: '字段4',
colProps: {
span: 8,
},
componentProps: {
|
Vben
authored
|
88
|
mode: 'multiple',
|
|
89
90
91
92
93
94
95
96
97
98
99
100
|
options: [
{
label: '选项1',
value: '1',
key: '1',
},
{
label: '选项2',
value: '2',
key: '2',
},
],
|
|
101
102
103
|
onChange: (value) => {
console.log(value, '123');
},
|
|
104
|
},
|
vben
authored
|
105
106
107
108
|
rules: [
{
required: true,
message: '请输入aa',
|
Vben
authored
|
109
|
type: 'array',
|
vben
authored
|
110
111
112
113
|
},
],
},
{
|
Vben
authored
|
114
|
field: 'field441',
|
vben
authored
|
115
116
117
118
119
120
121
122
123
124
|
component: 'Input',
label: '自定义校验',
colProps: {
span: 8,
},
rules: [
{
required: true,
// @ts-ignore
validator: async (rule, value) => {
|
vben
authored
|
125
|
if (!value) {
|
vben
authored
|
126
|
/* eslint-disable-next-line */
|
|
127
|
return Promise.reject('值不能为空');
|
vben
authored
|
128
|
}
|
vben
authored
|
129
|
if (value === '1') {
|
vben
authored
|
130
|
/* eslint-disable-next-line */
|
|
131
|
return Promise.reject('值不能为1');
|
vben
authored
|
132
133
134
|
}
return Promise.resolve();
},
|
vben
authored
|
135
|
trigger: 'change',
|
vben
authored
|
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
|
},
{
field: 'field5',
component: 'CheckboxGroup',
label: '字段5',
colProps: {
span: 8,
},
componentProps: {
options: [
{
label: '选项1',
value: '1',
},
{
label: '选项2',
value: '2',
},
],
},
rules: [{ required: true }],
},
{
field: 'field7',
component: 'RadioGroup',
label: '字段7',
colProps: {
span: 8,
},
componentProps: {
options: [
{
label: '选项1',
value: '1',
},
{
label: '选项2',
value: '2',
},
],
},
rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
},
|
|
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
|
{
field: 'field8',
component: 'Input',
label: '后端异步验证',
colProps: {
span: 8,
},
helpMessage: ['本字段演示异步验证', '本地规则:必须填写', '后端规则:不能包含admin'],
rules: [
{
required: true,
message: '请输入数据',
},
{
validator(_, value) {
return new Promise((resolve, reject) => {
isAccountExist(value)
.then(() => resolve())
.catch((err) => {
reject(err.message || '验证失败');
});
});
},
},
],
},
|
|
207
208
209
|
];
export default defineComponent({
|
vben
authored
|
210
|
components: { BasicForm, CollapseContainer, PageWrapper },
|
|
211
212
|
setup() {
const { createMessage } = useMessage();
|
Vben
authored
|
213
214
215
216
217
218
219
220
221
222
|
const [
register,
{ validateFields, clearValidate, getFieldsValue, resetFields, setFieldsValue },
] = useForm({
labelWidth: 120,
schemas,
actionColOptions: {
span: 24,
},
});
|
|
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
async function validateForm() {
try {
const res = await validateFields();
console.log('passing', res);
} catch (error) {
console.log('not passing', error);
}
}
async function resetValidate() {
clearValidate();
}
function getFormValues() {
const values = getFieldsValue();
createMessage.success('values:' + JSON.stringify(values));
}
function setFormValues() {
setFieldsValue({
|
vben
authored
|
240
|
field1: 1111,
|
|
241
|
field4: ['1'],
|
|
242
243
|
field5: ['1'],
field7: '1',
|
Vben
authored
|
244
245
|
field33: '2020-12-12',
field3: '2020-12-12',
|
|
246
247
248
249
250
251
252
253
254
255
256
257
|
});
}
return {
register,
schemas,
handleSubmit: (values: any) => {
createMessage.success('click search,values:' + JSON.stringify(values));
},
getFormValues,
setFormValues,
validateForm,
resetValidate,
|
Vben
authored
|
258
|
resetFields,
|
|
259
260
261
262
|
};
},
});
</script>
|