关于vue2.0 + ele的表单循环以及对应字段的验证!!!!!
html代码
<el-form ref="ruleForm" :inline="true" :model="form" label-width="140px" :rules="rules" v-loading.fullscreen.lock="fullScreenLoading" element-loading-text="拼命加载中..."> <div v-for="(item, index) in form.xh" @click="handleindex(index)"> <el-form-item label="账号" prop="tel"> <el-input v-model="item.tel" ></el-input> </el-form-item> <el-form-item label="姓名" prop="nickname" v-show="item.isnickname"> <el-input v-model="item.nickname"></el-input> </el-form-item> </div> <el-form-item label="年龄" prop="age"> <el-input v-model="form.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button> <el-button @click="onCancel">取消</el-button> </el-form-item> </el-form>
script代码
//引入接口 import { fetchdata, } from 'src/api/login' export default { var checkTel = (rule, value, callback) => { //循环判断这里是关键,这样做才可以对循环里每一条做判断 for (let i = 0; i < this.form.xh.length; i++) { value = this.form.xh[i].tel; var vdt = this.VDT.vdata(value, { //这里需要你自己去在另一个文件里配上 'required': {msg: '请填写账号'} }); if (!vdt.result) { callback(new Error(vdt.msg)); } else { callback(); } } }; var checkNickname = (rule, value, callback) => { for (let i = 0; i < this.form.xh.length; i++) { //先判断有没有姓名,假使nickname为false那就不要验证 if (this.form.xh[i].isNickname) { value = this.form.xh[i].nickname; var vdt = this.VDT.vdata(value, { //这里需要你自己去在另一个文件里配上 'required': {msg: '请填写姓名'} }); if (!vdt.result) { callback(new Error(vdt.msg)); } else { callback(); } } else { callback(); } } }; //因为这里不是循环里面的,所以value对应的值就一个故可直接使用 var checkAge = (rule, value, callback) => { var vdt = this.VDT.vdata(value, {'required': {msg: '请填写年龄'}}); if (!vdt.result) { callback(new Error(vdt.msg)); } else { callback(); } }; return { form: { xh:[{ tel, nickname, isnickname:false, //附一个初始值,默认不显示。 }], age:'', id:'1', }, rules: { tel: [{required: true, validator:checkTel,trigger: 'blur',}], nickname: [{required: true, validator:checkNickname,trigger: 'blur',}], age: [{required: true, validator:checkAge,trigger: 'blur',}], }, }, }, created() { this.getData(); }, methods: { getData() { this.fullScreenLoading = true; fetchdata(this.id).then(response => { //这里请求对应的接口 if(response.data.success == true) { this.fullScreenLoading = false; this.form = response.data.data; //返回数据赋给表单 }else{ this.fullScreenLoading = false; return false; } } } }
本文只是作者在开发时总结出来的经验,希望可以有所帮助。
以上这篇vue2.0 + ele的循环表单及验证字段方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。