友情提示:如果想需要el-table里面每一行的每个字段都需要进行校验的,请参参考这一下文章
ElementUI el-table动态增减行,通过el-from实现el-table行内输入验证-CSDN博客https://blog.csdn.net/snows_l/article/details/134050576?spm=1001.2014.3001.5502
一、一重数组的绑定
提示:
1、每一项el-form-item的prop以及rules的赋值以及data中form的结构(行内样式是为了复制直接看效果)
2、注意看users以及mobile的prop绑定的值不一样的,因此当只有一重数组时这两种写法都是可以的
-
:prop="'users[' + index + '].userName'"
-
:prop="'users.' + index + '.mobile'"
结构如下代码所示:
template
<!--
* @Description: ------ 文件描述 ------
* @Creater: snows_l snows_l@163.com
* @Date: 2023-02-12 14:02:00
* @LastEditors: snows_l snows_l@163.com
* @LastEditTime: 2023-02-12 14:24:00
* @FilePath: /vue2-js/src/components/elForm.vue
-->
<template>
<div class="container-warp">
<el-form :model="form" ref="form" :rules="rules" label-width="240px" :inline="false" size="normal">
<el-form-item label="联系人及手机号码">
<div class="name-phone">
<div
style="display: flex; align-items: center"
class="item"
v-for="(item, index) in form.users"
:key="item.id">
<el-form-item :prop="'users[' + index + '].userName'" :rules="rules.userName">
<el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
</el-form-item>
<el-form-item style="margin: 0 10px" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
<el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
</el-form-item>
<i
style="font-size: 20px"
class="icon"
:class="index == 0 ? 'el-icon-circle-plus-outline' : 'el-icon-circle-close'"
@click="addUser(index)"></i>
</div>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">立即创建</el-button>
<el-button>取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
script
<script>
export default {
name: '',
data() {
return {
form: {
users: [
{
userName: '',
mobile: null
}
],
otherInfn: null
},
rules: {
userName: [{ required: true, message: '请输入联系人' }],
mobile: [{ required: true, message: '请输入手机号' }]
}
};
},
created() {},
methods: {
onSubmit() {
console.log('-------- form --------', this.form);
this.$refs['form'].validate(valid => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
};
</script>
<style scoped>
.container-warp {
width: 100%;
height: 100%;
}
</style>
二、二重数组的绑定
提示:
1、注意看users以及mobile的prop绑定的值不一样的,因此当只有一重数组时这两种写法都是可以的
-
:prop="'users[' + index + '].list[' + i + '].attr2'"
-
:prop="'users.' + index + '.list.' + i + '.attr1'"
结构如下代码所示:
template
<template>
<div class="container-warp">
<span style="font-size: 16px; font-weight: 500">form表单的二重数组验证</span>
<el-form :model="form" ref="form" :rules="rules" label-width="240px" label-position="top" :inline="false" size="normal">
<div class="pingpu" style="display: flex; justify-content: space-around">
<el-form-item label="第一层数据1" prop="data1">
<el-input v-model="form.data1" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
<el-form-item label="第一层数据2" prop="data2">
<el-input v-model="form.data2" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
<el-form-item label="第一层数据3" prop="data3">
<el-input v-model="form.data3" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
</div>
<el-form-item label="基础信息users -- 一重数组">
<el-button type="primary" size="small" @click="handleUser">添加一个users</el-button>
<div class="name-phone">
<!-- 第一重数组 -->
<div class="item" v-for="(item, index) in form.users" :key="item.id">
<span>用户{{ index + 1 }}</span>
<div class="user-box" style="margin: 10px 0; padding: 20px; border: 1px solid #999">
<el-form-item :label="'联系人'" label-position="right" :prop="'users[' + index + '].userName'" :rules="rules.userName">
<el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
</el-form-item>
<el-form-item :label="'手机号'" label-position="right" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
<el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
</el-form-item>
<el-button type="primary" size="small" style="margin: 20px 0 10px 0" @click="handleUserOtherInfor(index)">添加用户{{ index + 1 }}其他信息</el-button>
<!-- 第二重数组 -->
<div class="other-info" style="border: 1px solid #eee">
<el-form-item label="其他信息List -- 二重数组" style="margin: 40px">
<div class="other-info-container" style="display: flex" v-for="(single, i) in form.users[index].list" :key="i">
<el-form-item style="margin-right: 20px" label="其他信息1" size="normal" :prop="'users.' + index + '.list.' + i + '.attr1'" :rules="rules.attr1">
<el-input v-model="single.attr1" size="small" placeholder="请输入其他信息1" />
</el-form-item>
<el-form-item label="其他信息2" size="normal" :prop="'users[' + index + '].list[' + i + '].attr2'" :rules="rules.attr2">
<el-input v-model="single.attr2" size="small" placeholder="请输入其他信息2" />
</el-form-item>
</div>
</el-form-item>
</div>
</div>
</div>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" size="small" @click="onSubmit">立即创建</el-button>
<el-button size="small">取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
script
<script>
export default {
name: '',
data() {
return {
form: {
data1: '',
data2: '',
data3: '',
users: [
{
userName: '',
mobile: null,
otherInfn: null,
list: [{ attr1: null, attr2: null }]
}
],
otherInfn: null
},
rules: {
data1: [{ required: true, message: '请输入第一层数据1' }],
data2: [{ required: true, message: '请输入第一层数据2' }],
data3: [{ required: true, message: '请输入第一层数据3' }],
userName: [{ required: true, message: '请输入联系人' }],
mobile: [{ required: true, message: '请输入手机号' }],
attr1: [{ required: true, message: '请输入其他信息1' }],
attr2: [{ required: true, message: '请输入其他信息2' }]
}
};
},
created() {},
methods: {
onSubmit() {
console.log('-------- form --------', this.form);
this.$refs['form'].validate(valid => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
handleUser() {
this.form.users.push({
userName: '',
mobile: null,
otherInfn: null,
list: [
{ attr1: null, attr2: null },
{ attr1: null, attr2: null }
]
});
},
handleUserOtherInfor(index) {
this.form.users[index].list.push({ attr1: null, attr2: null });
},
changeData(e) {
console.log('-------- e --------', e);
}
}
};
</script>
三、效果如如下:(二重数组的效果,就随便弄了下不要在乎样式[/手动狗头])
图1:初始效果
图2:校验的效果
图3:添加用户 -> 添加用户其他信息的效果
三、二重数组vue完整文件(二重数组也包含了一重数组的)文章来源:https://www.toymoban.com/news/detail-507657.html
<template>
<div class="container-warp">
<span style="font-size: 16px; font-weight: 500">form表单的二重数组验证</span>
<el-form :model="form" ref="form" :rules="rules" label-width="240px" label-position="top" :inline="false" size="normal">
<div class="pingpu" style="display: flex; justify-content: space-around">
<el-form-item label="第一层数据1" prop="data1">
<el-input v-model="form.data1" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
<el-form-item label="第一层数据2" prop="data2">
<el-input v-model="form.data2" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
<el-form-item label="第一层数据3" prop="data3">
<el-input v-model="form.data3" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
</el-form-item>
</div>
<el-form-item label="基础信息users -- 一重数组">
<el-button type="primary" size="small" @click="handleUser">添加一个users</el-button>
<div class="name-phone">
<!-- 第一重数组 -->
<div class="item" v-for="(item, index) in form.users" :key="item.id">
<span>用户{{ index + 1 }}</span>
<div class="user-box" style="margin: 10px 0; padding: 20px; border: 1px solid #999">
<el-form-item :label="'联系人'" label-position="right" :prop="'users[' + index + '].userName'" :rules="rules.userName">
<el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
</el-form-item>
<el-form-item :label="'手机号'" label-position="right" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
<el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
</el-form-item>
<el-button type="primary" size="small" style="margin: 20px 0 10px 0" @click="handleUserOtherInfor(index)">添加用户{{ index + 1 }}其他信息</el-button>
<!-- 第二重数组 -->
<div class="other-info" style="border: 1px solid #eee">
<el-form-item label="其他信息List -- 二重数组" style="margin: 40px">
<div class="other-info-container" style="display: flex" v-for="(single, i) in form.users[index].list" :key="i">
<el-form-item style="margin-right: 20px" label="其他信息1" size="normal" :prop="'users.' + index + '.list.' + i + '.attr1'" :rules="rules.attr1">
<el-input v-model="single.attr1" size="small" placeholder="请输入其他信息1" />
</el-form-item>
<el-form-item label="其他信息2" size="normal" :prop="'users[' + index + '].list[' + i + '].attr2'" :rules="rules.attr2">
<el-input v-model="single.attr2" size="small" placeholder="请输入其他信息2" />
</el-form-item>
</div>
</el-form-item>
</div>
</div>
</div>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" size="small" @click="onSubmit">立即创建</el-button>
<el-button size="small">取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
name: '',
data() {
return {
form: {
data1: '',
data2: '',
data3: '',
users: [
{
userName: '',
mobile: null,
otherInfn: null,
list: [{ attr1: null, attr2: null }]
}
],
otherInfn: null
},
rules: {
data1: [{ required: true, message: '请输入第一层数据1' }],
data2: [{ required: true, message: '请输入第一层数据2' }],
data3: [{ required: true, message: '请输入第一层数据3' }],
userName: [{ required: true, message: '请输入联系人' }],
mobile: [{ required: true, message: '请输入手机号' }],
attr1: [{ required: true, message: '请输入其他信息1' }],
attr2: [{ required: true, message: '请输入其他信息2' }]
}
};
},
created() {},
methods: {
onSubmit() {
console.log('-------- form --------', this.form);
this.$refs['form'].validate(valid => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
handleUser() {
this.form.users.push({
userName: '',
mobile: null,
otherInfn: null,
list: [
{ attr1: null, attr2: null },
{ attr1: null, attr2: null }
]
});
},
handleUserOtherInfor(index) {
this.form.users[index].list.push({ attr1: null, attr2: null });
},
changeData(e) {
console.log('-------- e --------', e);
}
}
};
</script>
<style scoped>
.container-warp {
padding: 20px;
}
.el-input {
max-width: 400px;
}
</style>
四、总结:
prop的绑定就有上文提到的这两种方式,因此不管是几重数组都是可以的(当然没有试过三重四重数组, 应该是可以的,当然项目中应该不会出现这种情况)文章来源地址https://www.toymoban.com/news/detail-507657.html
到了这里,关于el-form中数组(一重数组、二重数组以及多重数组)绑定检验的实例(prop以及rules的赋值)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!