iview 表单相关
Posted xfcao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iview 表单相关相关的知识,希望对你有一定的参考价值。
view表单验证的步骤:
第一步:给 Form 设置属性 rules :rules
第二步:同时给需要验证的每个 FormItem 设置属性 prop 指向对应字段即可 prop=”“
第三步:注意:Form标签里面是 :model
第四步:注意:在Form标签里面必须添加 ref,相当于id,在此范围内的表单验证有效
第五步:在操作保存按钮时,添加方法,对整个表单进行校验,参数为检验完的回调,会返回一个 Boolean 表示成功与失败
---------------------
<Form :label-width="100" ref=‘contractForm‘ :model=‘contractForm‘ :rules="ruleValidate">
<FormItem label=‘合同编号:‘ prop="contractNo">
<Input placeholder="请输入合同编号" v-model=‘contractForm.contractNo‘></Input>
</FormItem>
//data里面,写验证
ruleValidate: {
contractNo:[
{ required: true, message: ‘合同编号不能为空‘, trigger: ‘blur‘ },
],
}
//methods里面,写方法
addChange(name){
this.$refs[name].validate(valid => {
if (valid) {
}
});
</Form>
iview进行表单验证select时候验证失败的问题:
用iview自带的表单验证select标签的时候,一直验证不通过,因为iview默认校验数据类型为String,而我的select用的value是number类型的
ruleValidate: {
customer:[
{ required: true, message: ‘客户名称不能为空‘, trigger: ‘blur‘,type:‘number‘},
],
}
view进行表单验证时间日期验证失败的问题:
和下拉框一样,日期的类型是data
ruleValidate: {
advance:[
{ required: true, message: ‘预送达时间不能为空‘, trigger: ‘change‘ ,type: ‘date‘},
],
}
iview进行多重验证的写法:
多重验证包括第一要验不能为空,第二要验证限制的一些长度,写正则表达式等
ruleValidate: {
goodsNum: [
{ required: true, message: ‘数量不能为空‘, trigger: ‘blur‘ },
{ type: ‘string‘,pattern:/^(([1-9]d{0,3})|0)(.d{0,2})?$/, message:‘数量应为正浮点数且不超过9999.99‘, trigger:‘blur‘},
],
}
//高级配置验证
validateAdvancedFormItem: {
name: [
{required:
true
, message:
‘任务名称不能为空‘
, trigger:
‘blur‘
},
{type:
‘string‘
, max: 20, message:
‘不能超过20个字符‘
, trigger:
‘blur‘
},
{validator: validNameExist, trigger:
‘blur‘
}
],
groupId: [
{type:
‘string‘
, required:
true
, message:
‘请选择任务分组‘
, trigger:
‘change‘
}
],
keywords: [
{required:
true
, message:
‘关键词不能为空‘
, trigger:
‘blur‘
},
{type:
‘string‘
, max: 7000, message:
‘不能超过7000个字符‘
, trigger:
‘blur‘
},
{validator: validKeyWordsRule, trigger:
‘blur‘
}
],
/* chooseSiteGroupList: [//todo 暂时注释掉网站分组
{ required: true, type: ‘array‘, min: 1, message: ‘请选择网站分组‘, trigger: ‘change‘ },
],*/
chooseInfoTypeList: [
{required:
true
, type:
‘array‘
, min: 1, message:
‘请选择信息类型‘
, trigger:
‘change‘
},
],
warnNum: [
{required:
true
, message:
‘请填写预警增量‘
},
],
warnUserList: [
{required:
true
, type:
‘array‘
, message:
‘请选择预警人员‘
, validator: validatewarnUser, trigger:
‘change‘
},
],
},
//验证高级配置关键词 规则
const validKeyWordsRule = (rule, value, callback) => {
var
isExceedTwitenty =
this
.getAdvancedKeyWords();
var
isExceedThreeHundreand =
this
.getAdvancedKeyWords();
if
(isExceedTwitenty == 1) {
callback(
new
Error(
‘配置单个关键词长度不能超过20‘
))
}
else
if
(isExceedThreeHundreand == 2) {
callback(
new
Error(
‘配置关键词个数不能超过300‘
))
}
else
{
callback();
}
};
//处理关键词
getAdvancedKeyWords:
function
() {
var
flag = -1;
if
(
this
.dailyTaskItem.keywords !=
‘‘
&&
this
.dailyTaskItem.keywords.trim() !=
‘‘
) {
//判断单个配置的关键词长度是否大于20
var
str =
‘‘
;
for
(
var
i = 0; i <
this
.dailyTaskItem.keywords.length; i++) {
str = str +
this
.dailyTaskItem.keywords.substr(i, 1).replace(/[&|||!|(|)|"]/,
‘ ‘
);
}
var
keywordArr = str.split(
‘ ‘
);
var
resultArr = [];
for
(
var
i
in
keywordArr) {
if
(keywordArr[i] !=
‘‘
) {
resultArr.push(keywordArr[i])
if
(keywordArr[i].trim().length > 20) {
flag = 1;
break
}
}
}
//.关键词一共300个
if
(resultArr.length > 300) {
flag = 2;
}
}
return
flag;
},