# 开发规则
以开发手机号规则为例,自定义规则如何开发及如何注册到指定widget上?
# 规则示例
// rules/PhoneRule.js
export default class PhoneRule {
//给widget定义类型及名称,类型必须不能与已经存在的规则相同
// string,number,pattern,url,email
static get type () {
return 'phone'
}
static get name () {
return '手机号'
}
constructor (rule = {}) {
const defaultRule = {
// 类型参考:[async-validator](https://github.com/yiminghe/async-validator)
type: 'pattern',
pattern: '',
message: '手机号不正确'
}
// 原始规则描述,schema.rules字段
this.origin = Object.assign({}, defaultRule, rule)
// 常见规则的默认声明
this.rule = {
type: 'pattern',
trigger: 'blur',
pattern: /^1[3-9]\d{9}$/g,
message: ''
}
this.update(this.origin)
}
update (rule) {
if (rule) {
this.rule.message = rule.message
Object.assign(this.origin, rule)
}
}
}
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
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
# 注册到widget
import { render, helper, Rule } from 'epage-core'
import Epage from 'epage'
import widgets, { entry } from 'epage-iview'
import * as myRules from './rules'
// 添加自定义规则
Rule.set(myRules)
// 给input widget添加手机号规则,input属性面板添加新规则下拉中即可以看见
helper.setValidators(widgets, { input: ['phone'] })
// 创建设计器
new Epage({
el,
Rule,
pc: {
widgets,
component: entry,
Render: render.VueRender
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19