赞
踩
除了一些简单的表单验证之外,我们还会有一些稍微复杂点的多层级表单的验证,如下图所示可点击添加,删除对数组进行操作,当点击确定时需要验证每一条form-item
不能为空
其tempalte
部分主要代码如下所示:
<el-form :model="form" ref="cardForm" :rules="rules" label-position="top" > <el-form-item v-for="(item, index) in form.CARD_RIGHTS_LIST" :label="'卡权益' + (index+1)" :key="index" :prop="`CARD_RIGHTS_LIST.${index}.RIGHT_NAME`" :rules="{required: true, message: `请输入信用卡权益${index+1}`, trigger: ['change']}"> <el-col :span="20"> <el-input v-model=" item.RIGHT_NAME" :maxlength="20" placeholder="请输入信用卡权益" clearable show-word-limit></el-input> </el-col> <el-col :span="4" style="text-align: right;"> <el-button v-if="form.CARD_RIGHTS_LIST.length > 1" icon="el-icon-delete" type="danger" plain @click.prevent="handleRemoveValue(index)">删除</el-button> </el-col> </el-form-item> <el-form-item v-if="form.CARD_RIGHTS_LIST.length < max" class="last"> <div class="et-tag__button--add" @click="handleAddValue"><img :src="tagAddIcon" width="16px" height="16px" alt="">添加(最多{{max}}条)</div> </el-form-item> </el-form> <el-form-item class="product-form__footer last" label=""> <el-button class="et-button" type="primary" @click="submitForm(form)">确认</el-button> </el-form-item>
data
部分代码如下:
form:{
CARD_RIGHTS_LIST:[{ID:'',RIGHT_NAME:''}],
},
methods
部分如下:
//添加form-item handleAddValue() { this.form.CARD_RIGHTS_LIST.push({ID:'',RIGHT_NAME:""}) }, //删除form-item handleRemoveValue(index) { this.form.CARD_RIGHTS_LIST.splice(index, 1) }, //提交form并且验证 submitForm() { this.$refs.cardForm.validate((valid) => { if (valid) { this.$message.info("操作成功"); //此处加入自己需求 } else { //此处加入自己需求 } } else { this.$message.info("请完善信息"); return ; } }); },
以上仅展示此处对应的需求代码,可直接根据自己的需求进行修改即可实现此功能
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。