element form 动态增减表单项加验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="./jquery/jquery-3.2.1.js" type="text/javascript"></script> <script type="text/javascript" src="./vue/vue.js"></script> <link href="./elementUI/elementUI.css" type="text/css" rel="stylesheet" /> <!--对elementUI样式的修改--> <script src="./elementUI/elementUI.js" type="text/javascript"></script> <script src="./formadd.js"></script> <style> *{ padding: 0; margin: 0; } html,body{ width: 100%; height: 100%; } #vue_container { width: 100%; height: 100%; background-color: rgb(235, 237, 241); overflow: hidden; } .el-input{ width: auto; margin-right: 20px; } .demo-dynamic{ width: 52.0833vw; margin-top: 50px; } .col-mb{ margin-bottom: 1.8519vh; } </style> </head> <body> <div id="vue_container"> <!-- model 表单数据对象 --> <!-- prop 表单域 model 字段,在使用 validate、resetFields 方法的情况下,该属性是必填的 --> <el-form :model="dynamicValidateForm.formData" ref="dynamicValidateForm" label-width="200px" class="demo-dynamic"> <el-form-item prop="email" label="邮箱" :rules="[ { required: true, message: '请输入邮箱地址', trigger: 'blur' }, { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] } ]"> <el-input v-model="dynamicValidateForm.email"></el-input> </el-form-item> <el-form-item label="活动时间" required> <div class="aaaa" v-for="(domain, index) in dynamicValidateForm.formData.domains"> <el-col :span="8" class="col-mb"> <el-form-item :key="domain.key" :prop="'domains.' + index + '.value'" :rules="rules.value"> <el-input v-model.number="domain.value"></el-input> </el-form-item> </el-col> <el-col class="line" :span="2">-</el-col> <el-col :span="11" class="col-mb"> <el-form-item :key="domain.key + 'aaa'" :prop="'domains.' + index + '.value2'" :rules="rules.value2"> <el-input v-model.number="domain.value2"></el-input> <el-button @click.prevent="removeDomain(domain)">删除</el-button> </el-form-item> </el-col> </div> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button> <el-button @click="addDomain">新增域名</el-button> <el-button @click="resetForm('dynamicValidateForm')">重置</el-button> </el-form-item> </el-form> </div> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | var VueContainer = null ; $( function () { VueContainer = new Vue({ el: "#vue_container" , data() { var validatePass = (rule, value, callback) => { var field = rule.field var index = field.split( '.' )[1]; var end = this .dynamicValidateForm.formData.domains[index].value2; if (!Number.isInteger(value)) { return callback( new Error( '请输入数字值' )); } else if (value == '' || value == undefined) { return callback( new Error( '请选择' )); } else if (end != '' && end != undefined) { if (parseInt(value) > parseInt(end)) { return callback( new Error( '开始时间 大于了结束 的时间' )); } else { // this.resetValidate('domains' + index + 'value2') } } return callback(); }; var validatePass2 = (rule, value, callback) => { var field = rule.field var index = field.split( '.' )[1]; var start = this .dynamicValidateForm.formData.domains[index].value; if (!Number.isInteger(value)) { return callback( new Error( '请输入数字值' )); } else if (value == '' || value == undefined) { return callback( new Error( '请选择' )); } else if (start != '' && start != undefined) { if (value < start) { return callback( new Error( '开始时间 大于了结束 的时间22' )); } else { console.log( 'aaaadd ' ,); // this.resetValidate('domains' + index + 'value') } } return callback(); } return { dynamicValidateForm: { email: '' , formData: { domains: [{ value: '' , value2: '' }], } }, rules: { value: [ { validator: validatePass, trigger: 'blur' } ], value2: [ { validator: validatePass2, trigger: 'blur' } ], } } }, methods: { resetValidate(prop) { this .$refs.dynamicValidateForm.validateField(prop); }, // 提交 submitForm(formName) { this .$refs[formName].validate((valid) => { if (valid) { alert( 'submit!' ); } else { console.log( 'error submit!!' ); return false ; } }); }, // 重置 resetForm(formName) { this .$refs[formName].resetFields(); }, // 删除 removeDomain(item) { var index = this .dynamicValidateForm.formData.domains.indexOf(item) if (index !== -1) { this .dynamicValidateForm.formData.domains.splice(index, 1) } }, // 新增 addDomain() { this .dynamicValidateForm.formData.domains.push({ value: '' , value2: '' , key: Date.now() }); } }, }) }) |
效果如下:
分类:
element
标签:
Element+Form
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库