Example
// 全局模块引入
import { FormUtil, Validate } from '@ccreator/smart-ui';
// 最小化模块引入
import FormUtil from '@ccreator/smart-ui/dist/FormUtil';
const fields = [
{
name: 'user.name',
label: '用户名',
value: 'admin',
required: true,
validate: Validate.string().min(2).max(100),
},
{
name: 'account',
label: '账号',
required: true,
validate: Validate.string().min(2).max(100),
},
];
// 获取字段配置的默认值
const form = FormUtil.values(fields);
// 更新字段
const updated = FormUtil.update(fields, 'user.name', { value: 'admin' });
// 删除字段
const removed = FormUtil.remove(fields, 'user.name');
const removed = FormUtil.remove(fields, ['user.name', 'account']);
// 验证字段
const result = FormUtil.validate(fields, { user: { name: '张三' } });
Methods
(async, static) validate(fields, data)
校验字段(同Validate.validate)
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | 字段集合 |
data |
Object | 被验证的对象 |
(inner) omit(fields, name, keys)
删除字段的配置项
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | 字段集合 |
name |
String | 字段名称 |
keys |
Array | 配置项 |
Returns:
Array 修改后的字段集合
(inner) remove(fields, names)
删除字段
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | 字段集合 |
names |
String | Array | 要删除的字段名或字段名集合 |
Returns:
Array 删除后的字段集合
(inner) update(fields, name, data)
更新字段配置
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | 字段集合 |
name |
String | 字段名称 |
data |
Object | 更新的内容 |
Returns:
Array 修改后的字段集合
(inner) values(fields)
获取字段的默认值
Parameters:
Name | Type | Description |
---|---|---|
fields |
Array | 字段集合 |
Returns:
Object