Skip to content

Commit fc4ed8b

Browse files
committed
add flow validate.ts
1 parent 3e464a8 commit fc4ed8b

File tree

2 files changed

+150
-21
lines changed

2 files changed

+150
-21
lines changed

admin-ui/src/api/validate.ts

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import {FormInstance} from "antd/es/form/hooks/useForm";
2+
import {NamePath} from "rc-field-form/es/interface";
3+
4+
// 流程表单API 提供get post的能力
5+
export interface FlowFormApi {
6+
get: (url: string, params?: any) => Promise<any>;
7+
post: (url: string, data: any) => Promise<any>;
8+
}
9+
10+
// 流程表单验证内容
11+
export class FlowFormValidateContent {
12+
readonly value: any;
13+
readonly form: FormInstance;
14+
readonly api?: FlowFormApi
15+
16+
constructor(value: any, form: FormInstance, api?: FlowFormApi) {
17+
this.value = value;
18+
this.form = form;
19+
this.api = api;
20+
}
21+
}
22+
23+
// 自定义验证
24+
export interface FlowFormCustomValidate {
25+
name: NamePath;
26+
validate: (content: FlowFormValidateContent) => Promise<string[]>;
27+
}
28+
29+
// 流程表单API上下文
30+
export class FlowFormApiContext {
31+
32+
private static readonly instance: FlowFormApiContext = new FlowFormApiContext();
33+
34+
private api: FlowFormApi | undefined;
35+
36+
private constructor() {
37+
this.api = undefined;
38+
}
39+
40+
public static getInstance() {
41+
return FlowFormApiContext.instance;
42+
}
43+
44+
public setApi(api: FlowFormApi) {
45+
this.api = api;
46+
}
47+
48+
public getApi() {
49+
return this.api;
50+
}
51+
}
52+
53+
// 自定义验证上下文
54+
export class FlowFormCustomValidateContext {
55+
56+
private readonly map: Map<NamePath, FlowFormCustomValidate>;
57+
58+
constructor() {
59+
this.map = new Map();
60+
}
61+
62+
public addValidate(validate: FlowFormCustomValidate) {
63+
this.map.set(validate.name, validate);
64+
}
65+
66+
public addCustomFunctionCodeValidate(namePath:NamePath,validateFuncCode:string){
67+
const validateFunc = new Function('content', validateFuncCode);
68+
this.addValidate({
69+
name: namePath,
70+
validate: async (content) => {
71+
return validateFunc(content);
72+
}
73+
});
74+
}
75+
76+
public validate(form: FormInstance) {
77+
this.map.values().forEach((validate) => {
78+
const value = form.getFieldValue(validate.name);
79+
const content = new FlowFormValidateContent(value, form, FlowFormApiContext.getInstance().getApi());
80+
validate.validate(content).then((res) => {
81+
form.setFields(
82+
[
83+
{
84+
name: validate.name,
85+
errors: res,
86+
}
87+
]
88+
)
89+
}).catch((error) => {
90+
form.setFields(
91+
[
92+
{
93+
name: validate.name,
94+
errors: [error],
95+
}
96+
]
97+
)
98+
});
99+
});
100+
}
101+
}
102+
103+

admin-ui/src/pages/welcome/index.tsx

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
import React from 'react';
2-
import logo from '@/assets/logo.svg';
32
import './index.scss';
4-
import {useSelector} from "react-redux";
5-
import {RootState} from "@/store/Redux";
6-
import RoleControl from "@/utils/RoleControl";
73
import Page from "@/components/Layout/Page";
4+
import {Button, Form, Input} from "antd";
5+
import {FlowFormApiContext, FlowFormCustomValidateContext} from "@/api/validate";
6+
import * as api from "@/api"
7+
8+
FlowFormApiContext.getInstance().setApi({
9+
get: (url: string, params?: any) => {
10+
return api.get(url, params);
11+
},
12+
post: (url: string, data: any) => {
13+
return api.post(url, data);
14+
}
15+
});
816

917
const Index = () => {
1018

11-
const counter = useSelector((state: RootState) => state.counter.value);
12-
const username = localStorage.getItem('username');
19+
const [form] = Form.useForm();
20+
21+
const context = new FlowFormCustomValidateContext();
22+
23+
const validateFuncCode = `
24+
if (content.value) {
25+
return [];
26+
} else {
27+
return ["姓名不存在"];
28+
}
29+
`;
30+
context.addCustomFunctionCodeValidate(["user", "name"], validateFuncCode);
1331

1432
return (
1533
<Page enablePageContainer={true}>
16-
<div className="App">
17-
<header className="App-header">
18-
<img src={logo} className="App-logo" alt="logo"/>
19-
<p>
20-
hi {username} , Redux counter: {counter}, Roles: {RoleControl.roles().map(item => (
21-
<label
22-
key={item}
23-
style={{
24-
margin: '0 5px',
25-
padding: '5px',
26-
}}>{item}</label>
27-
))}
28-
</p>
29-
</header>
30-
</div>
34+
35+
<Form
36+
form={form}
37+
>
38+
39+
<Form.Item
40+
name={["user", "name"]}
41+
label={"姓名"}
42+
>
43+
<Input/>
44+
</Form.Item>
45+
</Form>
46+
47+
<Button onClick={() => {
48+
form.validateFields().then(res => {
49+
console.log(res);
50+
})
51+
}}>test1</Button>
52+
53+
<Button onClick={() => {
54+
context.validate(form);
55+
}}>test2</Button>
56+
3157
</Page>
3258
);
3359
}

0 commit comments

Comments
 (0)