当前位置: 代码迷 >> 综合 >> 工作笔记:elementUI实现数组(需要验证的数据是以数组的形式呈现)表单验证
  详细解决方案

工作笔记:elementUI实现数组(需要验证的数据是以数组的形式呈现)表单验证

热度:31   发布时间:2024-02-10 12:51:17.0
<el-form :model="couponForm" ref="couponForm" label-width="200px"><el-form-item v-for="(item,idx) in couponForm.actIdList" :label="'活动期间第' + (idx + 1) + '次下单得'" :prop="'actIdList['+idx+']'" :rules="{ required: true, message: '请选择'}"><el-selectv-model="couponForm.actIdList[idx]"filterableplaceholder="请选择"><el-option v-for="(sItem,i) in couponList" :key="sItem.actId" :label="sItem.actName" :value="sItem.actId"></el-option></el-select><el-button type="text" @click="deleted(idx)" class="deleted">删除</el-button></el-form-item >
</el-form >

如上代码:

1.rules写到v-for元素中::rules="{ required: true, message: '请选择'}"

2.prop部分(我这里的actIdList为数组,所以不需要写属性;如果是对象,直接写成 :prop="'actIdList.'+idx+'.xx'")::prop="'actIdList['+idx+']'"  或者  :prop="'actIdList.'+idx"

3.v-modal部分:v-model="couponForm.actIdList[idx]",如果是对象,写成v-model="item.xx"

这样就可以实现,每个el-form-item都可以实现对应的验证

注:如果需要动态新增,直接新增couponForm.actIdList值就行:this.couponForm.actIdList.push("")