tmagic-editor/docs/guide/advanced/tmagic-form.md
roymondchen 734e6a8ee6 feat(form): 支持 rules.typeMatch 按字段 type 校验值类型与选项匹配
新增可扩展的 typeMatch 校验能力,兼容 async-validator 规则体系,并补充文档与单元测试。
2026-07-13 15:52:56 +08:00

168 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# @tmagic/form
tmagic-editor的表单配置核心就是使用了 @tmagic/form 来作为渲染器。@tmagic/form 是一个 npm 包,可以安装它,在你想使用的地方单独使用。
@tmagic/form 接受一个表单配置,详细配置可参考[表单 api](../../api/form/form-props.md)。字段校验(含 `typeMatch` 类型匹配)见[表单校验](../../form-config/rules.md)。
## 安装
```bash
# 最新稳定版
$ npm install @tmagic/form
```
```bash
$ npm install @tmagic/element-plus-adapter @tmagic/design element-plus -S
```
## 快速上手
本节将介绍如何在项目中使用 @tmagic/form
### 引入 @tmagic/form
MagicForm 使用了 element-plus 组件库
在 main.js 中写入以下内容:
```javascript
import { createApp } from 'vue';
import ElementPlus from 'element-plus';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import TMagicDesign from '@tmagic/design';
import MagicElementPlusAdapter from '@tmagic/element-plus-adapter';
import MagicForm from '@tmagic/form';
import App from './App.vue';
const app = createApp(App);
app.use(ElementPlus, {
locale: zhCn,
});
app.use(TMagicDesign, MagicElementPlusAdapter);
app.use(MagicForm);
app.mount("#app");
```
也可在安装时传入自定义 `typeMatch` 规则,详见[表单校验 - 扩展自定义 type 规则](../../form-config/rules.md#扩展自定义-type-规则)
```javascript
app.use(MagicForm, {
typeMatchRules: {
'my-field': (value, { message }) => {
if (typeof value !== 'string') {
return message || 'my-field 应为字符串';
}
},
},
});
```
以上代码便完成了 @tmagic/form 的引入。需要注意的是Element Plus 的样式文件需要单独引入。
在 App.vue 中写入以下内容:
```html
<m-form :config="config" :init-values="initValue"></m-form>
<script>
export default {
data() {
return {
config: [
{
text: "文本",
name: "text",
},
{
type: "number",
text: "计数器",
name: "number",
},
{
type: "row",
items: [
{
type: "date",
text: "日期",
name: "date",
},
{
type: "checkbox",
text: "多选框",
name: "checkbox",
},
],
},
{
type: "fieldset",
name: "fieldset",
legend: "分组",
items: [
{
type: "select",
text: "下拉选项",
name: "select",
options: [
{ text: "选项1", value: 1 },
{ text: "选项2", value: 2 },
],
},
],
},
],
initValue: {
text: "文本",
number: 10,
fieldset: {
select: 1,
},
},
};
},
};
</script>
```
### 开始使用
至此,一个基于 Vue 和 @tmagic/form 的开发环境已经搭建完毕,现在就可以编写代码了。
### 示例
<demo-block type="form" :config="[{
text: '文本',
name: 'text'
}, {
type: 'number',
text: '计数器',
name: 'number'
}, {
type: 'row',
items: [{
type: 'date',
text: '日期',
name: 'date'
}, {
type: 'checkbox',
text: '多选框',
name: 'checkbox'
}]
}, {
type: 'fieldset',
name: 'fieldset',
legend: '分组',
items: [{
type: 'select',
text: '下拉选项',
name: 'select',
options: [
{ text: '选项1', value: 1 },
{ text: '选项2', value: 2 }
]
}]
}]">
</demo-block>