api.md 15 KB

Classes

FieldCheck
FieldCheck
FormItem

FormItem 表单验证类

Typedefs

errMessage : string

错误信息

checkFields : Array.<(string|RegExp)>

验证字段匹配项

checkRule : Object

规则对象

validatorFunctionstring

自定义验证函数

ruleItem : Object

验证规则对象

checkCode : number

验证码

formItemData : object

表单项数据

FieldCheck

Kind: global class
Properties

Name Type Description
ruleItems Array.<ruleItem> 验证规则
addRuleItem function 添加一条验证规则
verify function 检查表单是否符合规则

new FieldCheck()

表单字段验证类

Example

let fieldCheck = new FieldCheck();
fieldCheck.addRuleItem('rule1',['name'],[
   {
   type: 'string',
   minLength: 2,
   maxLength: 10,
   message: '姓名必须为2-10个字符'
   }
   ]);
   fieldCheck.addRuleItem('rule2',['age'],[
   {
   type: 'number',
   min: 18,
   max: 100,
   message: '年龄必须为18-100岁'
   }]);
   let errMsg = fieldCheck.verify({
   name: 'kindring',
   age: 18});
   console.log(errMsg);
   // null
   let errMsg = fieldCheck.verify({
   name: 'kindring',
   age: 17});
   console.log(errMsg);
   // 年龄必须为18-100岁

new FieldCheck([ruleItems])

Param Type Description
[ruleItems] Array.<ruleItem> 验证规则数组

fieldCheck._isEmpty(v) ⇒ boolean

判断是否为空

Kind: instance method of FieldCheck

Param
v

fieldCheck.buildRuleItem(checkFields, ruleArr) ⇒ ruleItem

构建验证规则

Kind: instance method of FieldCheck
Returns: ruleItem - 验证规则对象

Param Type Description
checkFields Array.<(string|RegExp)> 需要验证的字段
ruleArr Array.<(validatorFunction|checkRule)> 验证规则

fieldCheck.addRuleItem(ruleName, checkFields, ruleArr) ⇒ FieldCheck

添加一条验证规则

Kind: instance method of FieldCheck
Returns: FieldCheck - 返回当前对象

Param Type Description
ruleName string 验证规则名,用于区分
checkFields Array.<(string|RegExp)> 用于匹配字段的字符或者正则数组
ruleArr Array.<(validatorFunction|checkRule)> 验证规则

fieldCheck.getRuleItem(field) ⇒ ruleItem

获取验证规则

Kind: instance method of FieldCheck
Returns: ruleItem - 验证规则

Param Type Description
field string 字段名

fieldCheck.checkField(field, value) ⇒ Array.<(checkCode|errMessage)>

检查字段是否符合规则

Kind: instance method of FieldCheck
Returns: Array.<(checkCode|errMessage)> - 错误码或错误信息

Param Description
field 字段名
value 字段值

fieldCheck.verify(formObject, [isMustMatch]) ⇒ errMessage

检查表单是否符合规则

Kind: instance method of FieldCheck
Returns: errMessage - 错误码或错误信息

Param Type Description
formObject Object 需要检验的表单项 字段:值
[isMustMatch] 是否强制要求匹配规则

FieldCheck

Kind: global class

new FieldCheck()

表单字段验证类

Example

let fieldCheck = new FieldCheck();
fieldCheck.addRuleItem('rule1',['name'],[
   {
   type: 'string',
   minLength: 2,
   maxLength: 10,
   message: '姓名必须为2-10个字符'
   }
   ]);
   fieldCheck.addRuleItem('rule2',['age'],[
   {
   type: 'number',
   min: 18,
   max: 100,
   message: '年龄必须为18-100岁'
   }]);
   let errMsg = fieldCheck.verify({
   name: 'kindring',
   age: 18});
   console.log(errMsg);
   // null
   let errMsg = fieldCheck.verify({
   name: 'kindring',
   age: 17});
   console.log(errMsg);
   // 年龄必须为18-100岁

new FieldCheck([ruleItems])

Param Type Description
[ruleItems] Array.<ruleItem> 验证规则数组

fieldCheck._isEmpty(v) ⇒ boolean

判断是否为空

Kind: instance method of FieldCheck

Param
v

fieldCheck.buildRuleItem(checkFields, ruleArr) ⇒ ruleItem

构建验证规则

Kind: instance method of FieldCheck
Returns: ruleItem - 验证规则对象

Param Type Description
checkFields Array.<(string|RegExp)> 需要验证的字段
ruleArr Array.<(validatorFunction|checkRule)> 验证规则

fieldCheck.addRuleItem(ruleName, checkFields, ruleArr) ⇒ FieldCheck

添加一条验证规则

Kind: instance method of FieldCheck
Returns: FieldCheck - 返回当前对象

Param Type Description
ruleName string 验证规则名,用于区分
checkFields Array.<(string|RegExp)> 用于匹配字段的字符或者正则数组
ruleArr Array.<(validatorFunction|checkRule)> 验证规则

fieldCheck.getRuleItem(field) ⇒ ruleItem

获取验证规则

Kind: instance method of FieldCheck
Returns: ruleItem - 验证规则

Param Type Description
field string 字段名

fieldCheck.checkField(field, value) ⇒ Array.<(checkCode|errMessage)>

检查字段是否符合规则

Kind: instance method of FieldCheck
Returns: Array.<(checkCode|errMessage)> - 错误码或错误信息

Param Description
field 字段名
value 字段值

fieldCheck.verify(formObject, [isMustMatch]) ⇒ errMessage

检查表单是否符合规则

Kind: instance method of FieldCheck
Returns: errMessage - 错误码或错误信息

Param Type Description
formObject Object 需要检验的表单项 字段:值
[isMustMatch] 是否强制要求匹配规则

FormItem

FormItem 表单验证类

Kind: global class

new FormItem(object, [fieldCheck], [option])

表单项

Param Type Description
object object 表单项数据
[fieldCheck] FieldCheck 字段验证对象
[option] object 配置项

formItem.fieldCheck : FieldCheck

字段验证对象

Kind: instance property of FormItem

formItem.checkForm(form, isMustMatch) ⇒ boolean

检查表单项是否符合要求

Kind: instance method of FormItem

Param Type Description
form object 表单对象
isMustMatch 是否必须全部匹配到验证规则

FormItem.buildFormItem(object, field, formItemData, fieldCheck, isMustMatchRule) ⇒ string

检查表单项是否符合要求

Kind: static method of FormItem
Returns: string - errMsg 错误信息

Param Type Description
object object 表单项数据
field string 字段名
formItemData formItemData 表单项数据
fieldCheck FieldCheck 字段验证对象
isMustMatchRule boolean 表单字段是否必须匹配到验证规则

FormItem.initFormItemData(formObject)

初始化表单项数据

Kind: static method of FormItem

Param Type Description
formObject formObject 表单对象

errMessage : string

错误信息

Kind: global typedef

checkFields : Array.<(string|RegExp)>

验证字段匹配项

Kind: global typedef

checkRule : Object

规则对象

Kind: global typedef
Properties

Name Type Description
[type] string 类型
[min] number 最小值
[max] number 最大值
[length] number 长度
[regex] RegExp 正则表达式
[message] errMessage 错误信息
[require] boolean 是否必须
[minLength] number 最小长度
[maxLength] number 最大长度
[validator] validatorFunction 自定义验证函数

validatorFunction ⇒ string

自定义验证函数

Kind: global typedef
Returns: string - 返回错误信息或者 null

Param Type Description
value any 需要验证的值

ruleItem : Object

验证规则对象

Kind: global typedef
Properties

Name Type Description
checkFields Array.<string> 需要验证的字段
rules Array.<(validatorFunction|checkRule)> 验证规则

checkCode : number

验证码

Kind: global typedef
Properties

Name Type Description
code_pass 1 验证通过
code_notPass 2 验证不通过
code_notMatch 3 未匹配到验证规则

formItemData : object

表单项数据

Kind: global typedef
Properties

Name Type Description
[val] string 表单项值
[msg] string 表单项错误信息
[state] number 表单项状态 0 通过 1 通过 2 不通过
[showText] string 表单项显示文本,用于在某些
[label] string 表单项显示文本
[init] string 表单项初始值
[options] Array 表单项枚举值
[depend] string 依赖字段, 该项存在将使用依赖字段的option中的checkField字段进行匹配验证规则
[reCheckField] string 该表单项用于匹配规则的字段
[disables] Array 禁用项