File size: 7,455 Bytes
c9d0005 | 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | ---
order: 2
toc: content
mobile: false
group:
title: 高级用法
order: 1
---
# 表单校验
- 通过内置校验字段配置,实现简单校验逻辑
- 通过 rules 配置,实现复杂校验逻辑
- 通过 validateMessages 实现校验提示模版定制
### 一、内置校验
- required:必填
- max:最大长度 | 最大值
- min:最小长度 | 最小值
- format:url | email | image | color
```jsx
import React from 'react';
import FormRender, { useForm } from 'form-render';
const schema = {
type: 'object',
displayType: 'row',
properties: {
input1: {
title: '必填',
type: 'string',
required: true,
},
input2: {
title: '数字最大值',
type: 'number',
max: 2,
required: true
},
input3: {
title: '数字最小值',
type: 'number',
min: 10,
required: true,
},
input4: {
title: '字符最大长度',
type: 'string',
max: 2,
required: true,
},
input5: {
title: '字符最小长度',
type: 'string',
min: 10,
required: true,
},
input6: {
title: 'url 校验',
type: 'string',
required: true,
format: 'url',
},
input7: {
title: 'email 校验',
type: 'string',
required: true,
format: 'email',
},
input8: {
title: '图片格式校验',
type: 'string',
required: true,
format: 'image',
}
}
};
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
footer={true}
/>
)
};
```
### 二、Rules 校验
- 全面拥抱 Antd Form Rules
- 自定义校验 validator:做了一点小小的改变,validator 直接返回布尔值。
```jsx
import React from 'react';
import FormRender, { useForm } from 'form-render';
const schema = {
type: 'object',
displayType: 'row',
properties: {
input1: {
title: '正则表达式',
type: 'string',
required: true,
rules: [
{ pattern: '^[\u4E00-\u9FA5]+$', message: '请输入中文!' }
]
},
input2: {
title: '自定义校验',
type: 'string',
rules: [
{
validator: (_, value) => {
const pattern = '^[\u4E00-\u9FA5]+$';
const result = new RegExp(pattern).test(value);
return result;
// 或者是返回一个对象,用于动态设置 message 内容
// return {
// status: result,
// message: '请输入中文!',
// }
},
message: '请输入中文!'
}
]
}
}
};
export default () => {
const form = useForm();
return (
<FormRender
schema={schema}
form={form}
footer={true}
/>
)
};
```
### 三、子表单校验
自定义组件是一个子表单时,表单提交是无法触发子表单进行校验的,所以这种类型的子组件需要单独处理
```js
import { useImperativeHandle } from 'react';
const ChildForm = (props) => {
// 内部校验方法,异步校验请用 async、await 语法
const validator = async () => {
return true; // 返回 boolean 值,true 表示内部校验通过
// 如果需在外部显示子表单错误信息可以使用对象形式返回
// retrun { status: boolean, message: string };
};
useImperativeHandle(props.addons.fieldRef, () => {
// 将校验方法暴露出去,方便外部表单提交时,触发校验
return {
validator
};
});
return (
...// 表单渲染代码
);
}
export default ChildForm;
```
### 四、定制校验模版
- 全面拥抱 Antd Form Rules
- validateMessages:通过配置 validateMessages 定制校验模版,可以按需定制,定制模版会和默认校验模版进行合并处理
```jsx
import React from 'react';
import FormRender, { useForm } from 'form-render';
const schema = {
type: 'object',
displayType: 'row',
properties: {
input1: {
title: '地址(必填)',
type: 'string',
required: true,
}
}
};
export default () => {
const form = useForm();
const validateMessages = {
required: '${label} 不能为空',
};
return (
<FormRender
schema={schema}
form={form}
footer={true}
validateMessages={validateMessages}
/>
)
};
```
### 五、默认模版内容
- 中文模版
```Js
const typeTemplate = '${label}的类型不是${label}';
const validateMessagesCN = {
default: '${label}未通过校验',
required: '${label}必填',
whitespace: '${label}不能为空',
date: {
format: '${label}的格式错误',
parse: '${label}无法被解析',
invalid: '${label}数据不合法',
},
types: {
string: typeTemplate,
method: typeTemplate,
array: typeTemplate,
object: typeTemplate,
number: typeTemplate,
date: typeTemplate,
boolean: typeTemplate,
integer: typeTemplate,
float: typeTemplate,
regexp: typeTemplate,
email: typeTemplate,
url: typeTemplate,
hex: typeTemplate,
},
string: {
len: '${label}长度不是${len}',
min: '${label}长度不能小于${min}',
max: '${label}长度不能大于${max}',
range: '${label}长度需在${min}与${max}之间',
},
number: {
len: '${label}不等于${len}',
min: '${label}不能小于${min}',
max: '${label}不能大于${max}',
range: '${label}需在${min}与${max}之间',
},
array: {
len: '${label}长度不是${len}',
min: '${label}长度不能小于${min}',
max: '${label}长度不能大于${max}',
range: '${label}长度需在${min}与${max}之间',
},
pattern: {
mismatch: '${label}未通过正则判断${pattern}',
},
};
```
- 英文模版
```Js
const typeTemplate = "'${label}' is not a valid ${type}";
const validateMessages = {
default: "Validation error on field '${label}'",
required: "'${label}' is required",
enum: "'${label}' must be one of [${enum}]",
whitespace: "'${label}' cannot be empty",
date: {
format: "'${label}' is invalid for format date",
parse: "'${label}' could not be parsed as date",
invalid: "'${label}' is invalid date",
},
types: {
string: typeTemplate,
method: typeTemplate,
array: typeTemplate,
object: typeTemplate,
number: typeTemplate,
date: typeTemplate,
boolean: typeTemplate,
integer: typeTemplate,
float: typeTemplate,
regexp: typeTemplate,
email: typeTemplate,
url: typeTemplate,
hex: typeTemplate,
},
string: {
len: "'${label}' must be exactly ${len} characters",
min: "'${label}' must be at least ${min} characters",
max: "'${label}' cannot be longer than ${max} characters",
range: "'${label}' must be between ${min} and ${max} characters",
},
number: {
len: "'${label}' must equal ${len}",
min: "'${label}' cannot be less than ${min}",
max: "'${label}' cannot be greater than ${max}",
range: "'${label}' must be between ${min} and ${max}",
},
array: {
len: "'${label}' must be exactly ${len} in length",
min: "'${label}' cannot be less than ${min} in length",
max: "'${label}' cannot be greater than ${max} in length",
range: "'${label}' must be between ${min} and ${max} in length",
},
pattern: {
mismatch: "'${label}' does not match pattern ${pattern}",
},
};
``` |