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>
iview 表单验证不通过问题?的更多相关文章
- iview表单验证下拉框不通过问题
iview表单验证的步骤: 第一步:给 Form 设置属性 rules :rules 第二步:同时给需要验证的每个 FormItem 设置属性 prop 指向对应字段即可 prop=”“ 第三步:注意 ...
- iview表单验证不生效问题注意点
按照iview官网介绍写的form表单验证,但是无论填写与否都不进行校验,找了很久的原因,突然才发现一个关键的地方,一定要加props!!! https://blog.csdn.net/xuaner8 ...
- iview表单验证之正则验证、函数验证
iview表单验证之正则 正则验证: 代码: loginRules: { stringLength: [ { required: true, message: '该字段不能为空', trigger: ...
- 有关使用 iview 表单验证的问题
Vue的UI解决框架,element-UI, iview-UI 有关表单验证使用的是同一个插件,async-validator,有关这个插件的用法就不做赘述,但是在iview表单的使用中可能会用到验证 ...
- vue中使用iview表单验证时this指针问题
需求 使用iview,在提交时对值b进行验证,使其不能大于值a 实现 <Form ref="config" :model="config" :rules= ...
- iview 表单验证
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- iview表单验证trigger:'change,blur'
今天发现,如果设置select的trigger:'blur'就算选择之后还是边框是红色的,之后查了一下iview的文档,也没有找到准确的蚊子描述,只看到form那个组件其中有一个例子,大概是selec ...
- iview表单验证数字
验证输入字符串必须为数字 html: <FormItem label="兑换积分:" prop="exchangeIntegral"> <In ...
- iview表单验证--数字必填+校验
直接使用: { required: true, type:"integer", message:"请填写整数", trigger: "blur&quo ...
随机推荐
- centos(linux)-jdk配置
1.清理系统默认自带的jdk 在安装centos时,可能系统会默认安装了例如openjdk等,需要先手动卸载 先执行:rpm -qa | grep jdk (查看已经自带的jdk): 卸载命名:sud ...
- JavaScript里的类和继承(转)
转自: http://www.h5cn.com/js/jishu/2016/0121/17634.html js与大部分客户端语言有几点明显的不同: JS是 动态解释性语言,没有编译过程,它在程序运行 ...
- 内存泄漏之malloc替换方法
//内存泄漏之malloc替换方法 //内存泄漏之malloc替换方法#include "stdio.h"#include "stdlib.h" /*文件路径名 ...
- React Native 中 component 生命周期
React Native 中 component 生命周期 转自 csdn 子墨博客 http://blog.csdn.net/ElinaVampire/article/details/518136 ...
- 解决Eclipse中文文档注释错位-处女座的悲哀!
1.右键打开eclips属性 2.选择兼容性为win8,然后打开Eclipse即可解决 作者:醉烟 出处:https://www.cnblogs.com/WangLei2018/ 本文版权归作者 ...
- Flask Bug记录之JinJa2.exceptions.UndefinedError: 'sqlite3.Row object' has no attribute 'get'
源码 py文件定义db的工厂函数如下 def get_db(): if "db" not in g: g.db = sqlite3.connect( current_app.con ...
- typora的基本使用技巧汇总
typora的基本使用技巧汇总 链接: https://www.jianshu.com/p/380005c8f104
- Python中几个必须知道的函数
Python中自带了几个比较有意思的函数,一般在面试或者笔试基础的时候会问到,其中3个就是map.filter.reduce函数. 1.map(function, iterable) 它第一个要传的元 ...
- 在windows系统下打包linux平台运行的go程序
在windows系统下打包linux平台运行的go程序 1.先在main.go下打包成.exe可执行程序测试代码是否正确 //cd到main.go目录 go build //打包命令 如果打包成功则表 ...
- 【Python基础】06_Python中的函数
1.函数的定义 def 函数名(): 函数封装的代码 …… 注:函数前后应该保留两个空行 2.函数的使用 直接使用函数名()调用函数块. def say_hello(): print("He ...