iview 表单验证不通过问题?
项目需要,需要怂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> <Option value="shanghai">London</Option> <Option value="shenzhen">Sydney</Option> </Select> </FormItem> <FormItem label="日期"> <Row> <Col span="11"> <FormItem prop="date"> <DatePicker type="date" placeholder="Select date" v-model="formValidate.date"></DatePicker> </FormItem> </Col> <Col span="2" style="text-align: center">-</Col> <Col span="11"> <FormItem prop="time"> <TimePicker type="time" placeholder="Select time" v-model="formValidate.time"></TimePicker> </FormItem> </Col> </Row> </FormItem> <FormItem label="性别" prop="gender"> <RadioGroup v-model="formValidate.gender"> <Radio label="man">男</Radio> <Radio label="gril">女</Radio> </RadioGroup> </FormItem> <FormItem label="爱好" prop="interest"> <CheckboxGroup v-model="formValidate.interest"> <Checkbox label="Eat"></Checkbox> <Checkbox label="Sleep"></Checkbox> <Checkbox label="Run"></Checkbox> <Checkbox label="Movie"></Checkbox> </CheckboxGroup> </FormItem> <FormItem label="备注" prop="desc"> <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="Enter something..."></Input> </FormItem> <FormItem> <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button> <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button> </FormItem> </Form> </template> <script> export default { const valideTel = (rule, value, callback) => { var re = /^1[0-9]{10}/ if (value === '' || value === null) { callback(new Error('请输入手机号')) } else if (!re.test(value)) { callback(new Error('请输入正确手机号')) } else { callback() } } data () { return { formValidate: { mail: '', city: '', gender: 'male', interest: [], date: '', time: '', desc: '', tel:'' }, ruleValidate: { tel: [ { validator: valideTel, required: true, trigger: 'blur' } ], mail: [ { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' }, { type: 'email', message: 'Incorrect email format', trigger: 'blur' } ], city: [ { required: true, message: 'Please select the city', trigger: 'change' } ], gender: [ { required: true, message: 'Please select gender', trigger: 'change' } ], interest: [ { required: true, type: 'array', min: 1, message: 'Choose at least one hobby', trigger: 'change' }, { type: 'array', max: 2, message: 'Choose two hobbies at best', trigger: 'change' } ], date: [ { required: true, type: 'date', message: 'Please select the date', trigger: 'change' } ], time: [ { required: true, type: 'string', message: 'Please select time', trigger: 'change' } ], desc: [ { required: true, message: 'Please enter a personal introduction', trigger: 'blur' }, { type: 'string', min: 20, message: 'Introduce no less than 20 words', trigger: 'blur' } ] } } }, methods: { handleSubmit (name) { this.$refs[name].validate((valid) => { if (valid) { this.$Message.success('Success!'); } else { this.$Message.error('Fail!'); } }) }, handleReset (name) { this.$refs[name].resetFields(); } } } </script>