项目需要,需要怂iview.。使用一段时间感觉跟elementUI用起来差不多很方便。使用过程中遇到表单验证问题,如何避免在验证过程中偶尔出现验证不通过的异常情况?
《1》:给 <Form> 标签设置属性 :model = "fromdata" 与 :rules = "fromrules" 与 ref = "fromdatadom"
《2》:同时给需要验证的每个 <FormItem> 设置属性 prop ,同时在fromrules中定义该属性的校验规则
《3》:将需要验证的每个属性值定义在fromdata内,并:model 绑定在 input上。必须设置,某个值改变验证依然通不过
《4》:在操作保存按钮时,通过ref = "fromdatadom"设置某个范围内验证,参数为检验完的回调,会返回一个 Boolean 表示成功与失败
handleSubmit (name) {
this.$refs[name].validate((valid) => {
if (valid) {
this.$Message.success('Success!');
} else {
this.$Message.error('Fail!');
}
})
}
以上是验证的基本步骤,但是我做的过程中遇到的的问题是由于iview默认校验数据类型为string,而偶尔其他数据类型没注意。iview的type验证种类:
type: 'string', //iview默认校验数据类型为String
type: 'array',
type:'number'
type:'integer' //Must be of type number and an integer.
type:'float' //Must be of type number and a floating point number.
type:'boolean'
type:'object' //Must be of type object and not Array.isArray.
type:'array' //Must be an array as determined by Array.isArray.
type:'regexp' //正则
type: 'email',
type: 'date',
type:'url' //Must be of type url.
enum: Value must exist in the enum.
hex: Must be of type hex.
更多iview验证详情见:https://github.com/yiminghe/async-validator
<template> <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80"> <FormItem label="电话" prop="tel"> <Input v-model="formValidate.mail" placeholder="请输入号码"></Input> </FormItem> <FormItem label="邮箱" prop="mail"> <Input v-model="formValidate.mail" placeholder="Enter your e-mail"></Input> </FormItem> <FormItem label="城市" prop="city"> <Select v-model="formValidate.city" placeholder="Select your city"> <Option value="beijing">New York</Option>