Skip to content

Commit

Permalink
feat(demo): add async-validator demo
Browse files Browse the repository at this point in the history
添加表单使用后端接口异步验证的例子
  • Loading branch information
mynetfan committed Jul 21, 2021
1 parent 341bd63 commit 8b4b767
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
15 changes: 14 additions & 1 deletion mock/demo/system.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MockMethod } from 'vite-plugin-mock';
import { resultPageSuccess, resultSuccess } from '../_util';
import { resultError, resultPageSuccess, resultSuccess } from '../_util';

const accountList = (() => {
const result: any[] = [];
Expand Down Expand Up @@ -185,4 +185,17 @@ export default [
return resultSuccess(menuList);
},
},
{
url: '/basic-api/system/accountExist',
timeout: 500,
method: 'post',
response: ({ body }) => {
const { account } = body || {};
if (account && account.indexOf('admin') !== -1) {
return resultError('该字段不能包含admin');
} else {
return resultSuccess(`${account} can use`);
}
},
},
] as MockMethod[];
4 changes: 4 additions & 0 deletions src/api/demo/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { defHttp } from '/@/utils/http/axios';

enum Api {
AccountList = '/system/getAccountList',
IsAccountExist = '/system/accountExist',
DeptList = '/system/getDeptList',
setRoleStatus = '/system/setRoleStatus',
MenuList = '/system/getMenuList',
Expand All @@ -38,3 +39,6 @@ export const getAllRoleList = (params?: RoleParams) =>

export const setRoleStatus = (id: number, status: string) =>
defHttp.post({ url: Api.setRoleStatus, params: { id, status } });

export const isAccountExist = (account: string) =>
defHttp.post({ url: Api.IsAccountExist, params: { account } }, { errorMessageMode: 'none' });
33 changes: 30 additions & 3 deletions src/views/demo/form/RuleForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
import { CollapseContainer } from '/@/components/Container/index';
import { CollapseContainer } from '/@/components/Container';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageWrapper } from '/@/components/Page';
import { isAccountExist } from '/@/api/demo/system';
const schemas: FormSchema[] = [
{
Expand Down Expand Up @@ -120,11 +121,11 @@
validator: async (rule, value) => {
if (!value) {
/* eslint-disable-next-line */
return Promise.reject('值不能为空');
return Promise.reject('值不能为空');
}
if (value === '1') {
/* eslint-disable-next-line */
return Promise.reject('值不能为1');
return Promise.reject('值不能为1');
}
return Promise.resolve();
},
Expand Down Expand Up @@ -174,6 +175,32 @@
},
rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
},
{
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 || '验证失败');
});
});
},
},
],
},
];
export default defineComponent({
Expand Down
21 changes: 19 additions & 2 deletions src/views/demo/system/account/account.data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAllRoleList } from '/@/api/demo/system';
import { getAllRoleList, isAccountExist } from '/@/api/demo/system';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';

Expand Down Expand Up @@ -54,7 +54,24 @@ export const accountFormSchema: FormSchema[] = [
field: 'account',
label: '用户名',
component: 'Input',
required: true,
helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
rules: [
{
required: true,
message: '请输入用户名',
},
{
validator(_, value) {
return new Promise((resolve, reject) => {
isAccountExist(value)
.then(() => resolve())
.catch((err) => {
reject(err.message || '验证失败');
});
});
},
},
],
},
{
field: 'pwd',
Expand Down

0 comments on commit 8b4b767

Please sign in to comment.