-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathvalidate.tsx
203 lines (187 loc) · 6.28 KB
/
validate.tsx
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
/* eslint-disable react/prop-types */
import React from 'react';
import Form from 'rc-field-form';
import Input from './components/Input';
const { Field } = Form;
interface ErrorProps {
children?: React.ReactNode[];
}
const Error: React.FC<ErrorProps> = ({ children }) => (
<ul style={{ color: 'red' }}>
{children.map((error, i) => (
<li key={i}>{error}</li>
))}
</ul>
);
const FieldState = ({ form, name }) => {
const touched = form.isFieldTouched(name);
const validating = form.isFieldValidating(name);
return (
<div style={{ color: 'green', position: 'absolute', marginTop: -35, left: 200 }}>
{touched ? <span>Touched!</span> : null}
{validating ? <span>Validating!</span> : null}
</div>
);
};
export default () => {
const onFinish = (values: object) => {
console.log('Finish:', values);
};
return (
<div style={{ position: 'relative' }}>
<h3>Validate Form</h3>
<Form style={{ padding: 16 }} onFinish={onFinish}>
{(values, form) => {
const usernameError = form.getFieldError('username');
const passwordError = form.getFieldError('password');
const password2Error = form.getFieldError('password2');
const errors = form.getFieldsError();
if (errors) {
console.log('Render with Errors:', values, form.getFieldsError());
}
return (
<React.Fragment>
<Field name="username" rules={[{ required: true, groupId: 'GroupA', id: '1' }]}>
<Input
placeholder="Username"
onChange={({ target: { value } }) => {
console.log('Username change:', value);
}}
/>
</Field>
<FieldState form={form} name="username" />
<Error>{usernameError}</Error>
<Field
name="password"
rules={[
{ required: true, groupId: 'GroupA', id: '2' },
context => ({
groupId: 'GroupA',
id: '3',
validator(_, __, callback) {
if (context.isFieldTouched('password2')) {
context.validateFields(['password2']);
callback();
return;
}
callback();
},
}),
]}
>
<Input placeholder="Password" />
</Field>
<FieldState form={form} name="password" />
<Error>{passwordError}</Error>
<Field
name="password2"
rules={[
{ required: true, groupId: 'GroupB', id: '4' },
context => ({
groupId: 'GroupB',
id: '5',
validator(rule, value, callback) {
const { password } = context.getFieldsValue(true);
if (password !== value) {
callback('Not Same as password1!!!');
}
callback();
},
}),
]}
>
<Input placeholder="Password 2" />
</Field>
<FieldState form={form} name="password2" />
<Error>{password2Error}</Error>
<Field name="renderProps" rules={[{ required: true, id: '6' }]}>
{(control, meta) => (
<div>
Use Meta:
<Input {...control} placeholder="render props" />
<FieldState form={form} name="renderProps" />
<Error>{meta.errors}</Error>
</div>
)}
</Field>
<Field
name="validateTrigger"
validateTrigger={['onSubmit', 'onChange']}
rules={[
{ required: true, validateTrigger: 'onSubmit', id: '7' },
{
id: '8',
validator(rule, value, callback) {
setTimeout(() => {
if (Number(value).toString() === value) {
callback();
}
callback('Integer number only!');
}, 1000);
},
validateTrigger: 'onChange',
},
]}
>
{(control, meta) => (
<div>
Multiple `validateTrigger`:
<ul>
<li>Required check on submit</li>
<li>Number check on change</li>
</ul>
<Input {...control} placeholder="validateTrigger" />
<FieldState form={form} name="validateTrigger" />
<Error>{meta.errors}</Error>
</div>
)}
</Field>
<br />
<button
type="button"
onClick={() => {
form.validateFields();
}}
>
Validate All
</button>
<button
type="button"
onClick={() => {
form.validateFields({
groupId: 'GroupA',
});
}}
>
Validate GroupA
</button>
<button
type="button"
onClick={() => {
form.validateFields({
groupId: 'GroupB',
});
}}
>
Validate GroupB
</button>
<button
type="button"
onClick={() => {
form.validateFields({
ignore: (rule) => {
return (Number(rule.id) % 2) === 0
}
});
}}
>
Validate Logical
</button>
<button type="submit">Submit</button>
</React.Fragment>
);
}}
</Form>
</div>
);
};