319 lines
8.1 KiB
Markdown
319 lines
8.1 KiB
Markdown
# 表单高级模式
|
|
|
|
## 目录
|
|
|
|
- [验证规则 (verifyForm)](#验证规则-verifyform)
|
|
- [字段联动](#字段联动)
|
|
- [自定义按钮](#自定义按钮)
|
|
- [打印模板](#打印模板)
|
|
|
|
---
|
|
|
|
## 源码位置
|
|
|
|
```
|
|
$ccWorkDir/web/apps/jnpf-web-apps-main/src/views/extend/formDemo/
|
|
```
|
|
|
|
---
|
|
|
|
## 验证规则 (verifyForm)
|
|
|
|
### 源码
|
|
|
|
```
|
|
formDemo/verifyForm/index.vue
|
|
formDemo/verifyForm1/index.vue
|
|
```
|
|
|
|
### 核心模式
|
|
|
|
```typescript
|
|
const state = reactive({
|
|
dataForm: {
|
|
account: '',
|
|
password: '',
|
|
email: '',
|
|
url: '',
|
|
creditcard: '',
|
|
tel: '',
|
|
mobile: '',
|
|
phone: '',
|
|
age: '',
|
|
zipCode: '',
|
|
cardId: '',
|
|
RGB: '',
|
|
wechat: '',
|
|
licensePlate: '',
|
|
qq: '',
|
|
ip: '',
|
|
english: '',
|
|
chinese: '',
|
|
date: '',
|
|
floatLtZero: '',
|
|
intGtZero: '',
|
|
intNEqZero: ''
|
|
},
|
|
dataRule: {
|
|
// 必填
|
|
account: [
|
|
{ required: true, message: '用户账号不能为空', trigger: 'blur' },
|
|
{ min: 6, max: 20, message: '长度在6-20之间', trigger: 'blur' }
|
|
],
|
|
// 自定义正则
|
|
password: [
|
|
{ required: true, message: '用户密码不能为空', trigger: 'blur' },
|
|
{ pattern: /^[\w-]{6,20}$/, message: '密码格式错误', trigger: 'blur' }
|
|
],
|
|
// 邮箱验证
|
|
email: [
|
|
{ pattern: /^[a-z0-9]+([._\\-]*[a-z-9])*@([a-z0-9][-a-z0-9]*[a-z0-9].){1,63}[a-z0-9]+$/, message: '请输入正确格式的电子邮件', trigger: 'blur' }
|
|
],
|
|
// URL 验证
|
|
url: [
|
|
{ pattern: /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)?@)?/, message: '请输入正确格式的URL', trigger: 'blur' }
|
|
],
|
|
// 信用卡验证
|
|
creditcard: [
|
|
{ pattern: /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$/, message: '请输入正确的信用卡号', trigger: 'blur' }
|
|
],
|
|
// 电话号码
|
|
tel: [
|
|
{ pattern: /^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的电话号码', trigger: 'blur' }
|
|
],
|
|
// 手机号码
|
|
mobile: [
|
|
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
|
|
],
|
|
// 座机
|
|
phone: [
|
|
{ pattern: /^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的座机号', trigger: 'blur' }
|
|
],
|
|
// 年龄
|
|
age: [
|
|
{ pattern: /^(?:[1-9][0-9]?|1[01][0-9]|120)$/, message: '请输入正确的年龄', trigger: 'blur' }
|
|
],
|
|
// 邮编
|
|
zipCode: [
|
|
{ pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮编', trigger: 'blur' }
|
|
],
|
|
// 身份证
|
|
cardId: [
|
|
{ pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号', trigger: 'blur' }
|
|
],
|
|
// RGB颜色
|
|
RGB: [
|
|
{ pattern: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, message: '请输入正确的RGB颜色值', trigger: 'blur' }
|
|
],
|
|
// 微信
|
|
wechat: [
|
|
{ pattern: /^[a-zA-Z][a-zA-Z0-9_-]{5,19}$/, message: '请输入正确的微信号', trigger: 'blur' }
|
|
],
|
|
// 车牌号
|
|
licensePlate: [
|
|
{ pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4,5}[A-Z0-9挂学警港澳]$/, message: '请输入正确的车牌号', trigger: 'blur' }
|
|
],
|
|
// QQ号
|
|
qq: [
|
|
{ pattern: /^[1-9][0-9]{4,10}$/, message: '请输入正确的QQ号', trigger: 'blur' }
|
|
],
|
|
// IP地址
|
|
ip: [
|
|
{ pattern: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/, message: '请输入正确的IP地址', trigger: 'blur' }
|
|
],
|
|
// 英文
|
|
english: [
|
|
{ pattern: /^[A-Za-z]+$/, message: '请输入英文字母', trigger: 'blur' }
|
|
],
|
|
// 中文
|
|
chinese: [
|
|
{ pattern: /^[\u4e00-\u9fa5]+$/, message: '请输入中文字符', trigger: 'blur' }
|
|
],
|
|
// 日期
|
|
date: [
|
|
{ pattern: /^\d{4}-\d{2}-\d{2}$/, message: '请输入正确格式的日期', trigger: 'blur' }
|
|
],
|
|
// 负数
|
|
floatLtZero: [
|
|
{ pattern: /^-\d+(\.\d+)?$/, message: '请输入负数', trigger: 'blur' }
|
|
],
|
|
// 正整数
|
|
intGtZero: [
|
|
{ pattern: /^[1-9]\d*$/, message: '请输入正整数', trigger: 'blur' }
|
|
],
|
|
// 非零整数
|
|
intNEqZero: [
|
|
{ pattern: /^-?[1-9]\d*$/, message: '请输入非零整数', trigger: 'blur' }
|
|
]
|
|
}
|
|
});
|
|
```
|
|
|
|
### 常用验证规则
|
|
|
|
| 规则 | 正则表达式 | 说明 |
|
|
|------|-----------|------|
|
|
| 必填 | `{ required: true }` | 不能为空 |
|
|
| 长度 | `{ min: 6, max: 20 }` | 字符长度限制 |
|
|
| 邮箱 | `/^[a-z0-9]+([._\\-]*[a-z-9])*@.../` | 邮箱格式 |
|
|
| 手机 | `/^1[3-9]\d{9}$/` | 手机号(1开头) |
|
|
| 电话 | `/^0\d{2,3}-?\d{7,8}$/` | 座机号码 |
|
|
| 身份证 | `/(^\d{15}$)\|(^\d{18}$).../` | 15/18位身份证 |
|
|
| URL | `/^(https?|ftp):\/\/.../` | 网址 |
|
|
| 邮编 | `/^[1-9]\d{5}$/` | 国内邮编 |
|
|
| 银行卡 | `/^(?:4[0-9]{12}...` | 信用卡/借记卡 |
|
|
| IP地址 | `/^(?:(?:25[0-5]...` | IPv4 |
|
|
| 中文 | `/^[\u4e00-\u9fa5]+$/` | 纯中文 |
|
|
| 英文 | `/^[A-Za-z]+$/` | 纯英文 |
|
|
| 正整数 | `/^[1-9]\d*$/` | 大于0的整数 |
|
|
|
|
### 自定义验证器
|
|
|
|
```typescript
|
|
// 密码确认验证
|
|
const validatePass = (_rule, value) => {
|
|
return value === state.dataForm.password
|
|
? Promise.resolve()
|
|
: Promise.reject(new Error('两次密码输入不一致'));
|
|
};
|
|
|
|
// 使用
|
|
{ validator: validatePass, trigger: 'blur' }
|
|
```
|
|
|
|
---
|
|
|
|
## 字段联动
|
|
|
|
### 源码
|
|
|
|
```
|
|
formDemo/fieldForm4/index.vue
|
|
formDemo/fieldForm5/index.vue
|
|
formDemo/fieldForm6/index.vue
|
|
```
|
|
|
|
### 核心模式
|
|
|
|
```typescript
|
|
// 根据一个字段的值控制另一个字段的显示/选项
|
|
watch(() => state.dataForm.selectType, (val) => {
|
|
if (val === '1') {
|
|
state.dataForm.inputValue = '类型1的默认值';
|
|
} else if (val === '2') {
|
|
state.options = [
|
|
{ label: '选项A', value: 'a' },
|
|
{ label: '选项B', value: 'b' }
|
|
];
|
|
}
|
|
});
|
|
|
|
// 计算字段
|
|
const computedValue = computed(() => {
|
|
return state.dataForm.price * state.dataForm.quantity;
|
|
});
|
|
```
|
|
|
|
### 联动场景
|
|
|
|
1. **选项联动**: 选择A时,B的选项跟着变
|
|
2. **值联动**: 选择A时,B自动填充默认值
|
|
3. **显示联动**: 选择A时,显示/隐藏某些字段
|
|
4. **计算联动**: 多个字段计算得出结果
|
|
|
|
---
|
|
|
|
## 自定义按钮
|
|
|
|
### workSystem 示例
|
|
|
|
```
|
|
表单自定义按钮及多打印模板示例 (469136940843723909)
|
|
```
|
|
|
|
### 核心模式
|
|
|
|
```typescript
|
|
// 按钮定义
|
|
const buttonList = [
|
|
{ title: '提交', icon: 'icon-ym icon-ym-generator-add', type: 'primary', click: 'handleSubmit' },
|
|
{ title: '保存草稿', icon: 'icon-ym icon-ym-generator-baocun', click: 'handleDraft' },
|
|
{ title: '取消', icon: 'icon-ym icon-ym-generator-guanbi', click: 'handleCancel' }
|
|
];
|
|
|
|
// 按钮点击处理
|
|
function handleSubmit() {
|
|
// 提交逻辑
|
|
}
|
|
|
|
function handleDraft() {
|
|
// 保存草稿
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 打印模板
|
|
|
|
### workSystem 示例
|
|
|
|
```
|
|
套打示例 (585384793399624261)
|
|
表单自定义按钮及多打印模板示例 (469136940843723909)
|
|
```
|
|
|
|
### 核心模式
|
|
|
|
```typescript
|
|
// 打印配置
|
|
const printConfig = {
|
|
printTemplate: 'default', // 模板名称
|
|
printFields: ['field1', 'field2'], // 打印字段
|
|
printType: '套打/普通打印'
|
|
};
|
|
|
|
// 打印处理
|
|
function handlePrint() {
|
|
// 调用打印组件
|
|
openPrintModal(true, { data: state.dataForm });
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 工作流表单
|
|
|
|
### 基本信息
|
|
|
|
工作流表单与普通表单不同:
|
|
- `enableFlow: 1`: 启用流程
|
|
- `webType: 2`: 表单列表类型
|
|
- `formData`: 流程字段列表
|
|
|
|
### 字段结构
|
|
|
|
```typescript
|
|
// 工作流表单字段 (不同于普通表单)
|
|
const flowFields = [
|
|
{ filedName: "流程标题", filedId: "flowTitle", required: true, tableName: "wform_xxx" },
|
|
{ filedName: "紧急情况", filedId: "flowUrgent", required: true, tableName: "wform_xxx" },
|
|
{ filedName: "申请人员", filedId: "applicant", required: true, tableName: "wform_xxx" }
|
|
];
|
|
```
|
|
|
|
### workSystem 中的工作流表单
|
|
|
|
| 表单名称 | 表单ID | 说明 |
|
|
|----------|--------|------|
|
|
| 请假申请 | 1589964488616407041 | 流程标题/紧急情况/申请人 |
|
|
| 销售订单 | 1589960344887267330 | 销售流程 |
|
|
| 订单示例 | 359757745991204532 | CRM订单流程 |
|
|
|
|
---
|
|
|
|
## 下一步
|
|
|
|
- [workSystem 示例索引](12-examples/workSystem-examples.md)
|
|
- [extend 示例索引](12-examples/extend-examples.md)
|
|
- [控件速查](01-quick-ref.md)
|