Commit 8b4b767f4ca78f7c6f7586d8ba662552c2b7bb51
1 parent
341bd633
feat(demo): add `async-validator` demo
添加表单使用后端接口异步验证的例子
Showing
4 changed files
with
67 additions
and
6 deletions
mock/demo/system.ts
1 | import { MockMethod } from 'vite-plugin-mock'; | 1 | import { MockMethod } from 'vite-plugin-mock'; |
2 | -import { resultPageSuccess, resultSuccess } from '../_util'; | 2 | +import { resultError, resultPageSuccess, resultSuccess } from '../_util'; |
3 | 3 | ||
4 | const accountList = (() => { | 4 | const accountList = (() => { |
5 | const result: any[] = []; | 5 | const result: any[] = []; |
@@ -185,4 +185,17 @@ export default [ | @@ -185,4 +185,17 @@ export default [ | ||
185 | return resultSuccess(menuList); | 185 | return resultSuccess(menuList); |
186 | }, | 186 | }, |
187 | }, | 187 | }, |
188 | + { | ||
189 | + url: '/basic-api/system/accountExist', | ||
190 | + timeout: 500, | ||
191 | + method: 'post', | ||
192 | + response: ({ body }) => { | ||
193 | + const { account } = body || {}; | ||
194 | + if (account && account.indexOf('admin') !== -1) { | ||
195 | + return resultError('该字段不能包含admin'); | ||
196 | + } else { | ||
197 | + return resultSuccess(`${account} can use`); | ||
198 | + } | ||
199 | + }, | ||
200 | + }, | ||
188 | ] as MockMethod[]; | 201 | ] as MockMethod[]; |
src/api/demo/system.ts
@@ -14,6 +14,7 @@ import { defHttp } from '/@/utils/http/axios'; | @@ -14,6 +14,7 @@ import { defHttp } from '/@/utils/http/axios'; | ||
14 | 14 | ||
15 | enum Api { | 15 | enum Api { |
16 | AccountList = '/system/getAccountList', | 16 | AccountList = '/system/getAccountList', |
17 | + IsAccountExist = '/system/accountExist', | ||
17 | DeptList = '/system/getDeptList', | 18 | DeptList = '/system/getDeptList', |
18 | setRoleStatus = '/system/setRoleStatus', | 19 | setRoleStatus = '/system/setRoleStatus', |
19 | MenuList = '/system/getMenuList', | 20 | MenuList = '/system/getMenuList', |
@@ -38,3 +39,6 @@ export const getAllRoleList = (params?: RoleParams) => | @@ -38,3 +39,6 @@ export const getAllRoleList = (params?: RoleParams) => | ||
38 | 39 | ||
39 | export const setRoleStatus = (id: number, status: string) => | 40 | export const setRoleStatus = (id: number, status: string) => |
40 | defHttp.post({ url: Api.setRoleStatus, params: { id, status } }); | 41 | defHttp.post({ url: Api.setRoleStatus, params: { id, status } }); |
42 | + | ||
43 | +export const isAccountExist = (account: string) => | ||
44 | + defHttp.post({ url: Api.IsAccountExist, params: { account } }, { errorMessageMode: 'none' }); |
src/views/demo/form/RuleForm.vue
@@ -15,9 +15,10 @@ | @@ -15,9 +15,10 @@ | ||
15 | <script lang="ts"> | 15 | <script lang="ts"> |
16 | import { defineComponent } from 'vue'; | 16 | import { defineComponent } from 'vue'; |
17 | import { BasicForm, FormSchema, useForm } from '/@/components/Form/index'; | 17 | import { BasicForm, FormSchema, useForm } from '/@/components/Form/index'; |
18 | - import { CollapseContainer } from '/@/components/Container/index'; | 18 | + import { CollapseContainer } from '/@/components/Container'; |
19 | import { useMessage } from '/@/hooks/web/useMessage'; | 19 | import { useMessage } from '/@/hooks/web/useMessage'; |
20 | import { PageWrapper } from '/@/components/Page'; | 20 | import { PageWrapper } from '/@/components/Page'; |
21 | + import { isAccountExist } from '/@/api/demo/system'; | ||
21 | 22 | ||
22 | const schemas: FormSchema[] = [ | 23 | const schemas: FormSchema[] = [ |
23 | { | 24 | { |
@@ -120,11 +121,11 @@ | @@ -120,11 +121,11 @@ | ||
120 | validator: async (rule, value) => { | 121 | validator: async (rule, value) => { |
121 | if (!value) { | 122 | if (!value) { |
122 | /* eslint-disable-next-line */ | 123 | /* eslint-disable-next-line */ |
123 | - return Promise.reject('值不能为空'); | 124 | + return Promise.reject('值不能为空'); |
124 | } | 125 | } |
125 | if (value === '1') { | 126 | if (value === '1') { |
126 | /* eslint-disable-next-line */ | 127 | /* eslint-disable-next-line */ |
127 | - return Promise.reject('值不能为1'); | 128 | + return Promise.reject('值不能为1'); |
128 | } | 129 | } |
129 | return Promise.resolve(); | 130 | return Promise.resolve(); |
130 | }, | 131 | }, |
@@ -174,6 +175,32 @@ | @@ -174,6 +175,32 @@ | ||
174 | }, | 175 | }, |
175 | rules: [{ required: true, message: '覆盖默认生成的校验信息' }], | 176 | rules: [{ required: true, message: '覆盖默认生成的校验信息' }], |
176 | }, | 177 | }, |
178 | + { | ||
179 | + field: 'field8', | ||
180 | + component: 'Input', | ||
181 | + label: '后端异步验证', | ||
182 | + colProps: { | ||
183 | + span: 8, | ||
184 | + }, | ||
185 | + helpMessage: ['本字段演示异步验证', '本地规则:必须填写', '后端规则:不能包含admin'], | ||
186 | + rules: [ | ||
187 | + { | ||
188 | + required: true, | ||
189 | + message: '请输入数据', | ||
190 | + }, | ||
191 | + { | ||
192 | + validator(_, value) { | ||
193 | + return new Promise((resolve, reject) => { | ||
194 | + isAccountExist(value) | ||
195 | + .then(() => resolve()) | ||
196 | + .catch((err) => { | ||
197 | + reject(err.message || '验证失败'); | ||
198 | + }); | ||
199 | + }); | ||
200 | + }, | ||
201 | + }, | ||
202 | + ], | ||
203 | + }, | ||
177 | ]; | 204 | ]; |
178 | 205 | ||
179 | export default defineComponent({ | 206 | export default defineComponent({ |
src/views/demo/system/account/account.data.ts
1 | -import { getAllRoleList } from '/@/api/demo/system'; | 1 | +import { getAllRoleList, isAccountExist } from '/@/api/demo/system'; |
2 | import { BasicColumn } from '/@/components/Table'; | 2 | import { BasicColumn } from '/@/components/Table'; |
3 | import { FormSchema } from '/@/components/Table'; | 3 | import { FormSchema } from '/@/components/Table'; |
4 | 4 | ||
@@ -54,7 +54,24 @@ export const accountFormSchema: FormSchema[] = [ | @@ -54,7 +54,24 @@ export const accountFormSchema: FormSchema[] = [ | ||
54 | field: 'account', | 54 | field: 'account', |
55 | label: '用户名', | 55 | label: '用户名', |
56 | component: 'Input', | 56 | component: 'Input', |
57 | - required: true, | 57 | + helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'], |
58 | + rules: [ | ||
59 | + { | ||
60 | + required: true, | ||
61 | + message: '请输入用户名', | ||
62 | + }, | ||
63 | + { | ||
64 | + validator(_, value) { | ||
65 | + return new Promise((resolve, reject) => { | ||
66 | + isAccountExist(value) | ||
67 | + .then(() => resolve()) | ||
68 | + .catch((err) => { | ||
69 | + reject(err.message || '验证失败'); | ||
70 | + }); | ||
71 | + }); | ||
72 | + }, | ||
73 | + }, | ||
74 | + ], | ||
58 | }, | 75 | }, |
59 | { | 76 | { |
60 | field: 'pwd', | 77 | field: 'pwd', |