Home >Web Front-end >JS Tutorial >About the form item verification method in the v-for loop when vue uses the Element component
This article mainly introduces the form item verification method in the v-for loop when Vue uses the Element component. The content is quite good. I will share it with you now and give it as a reference.
The title description looks a bit complicated, with vue, Element, form validation, and v-for loop? Isn’t it a little messy? However, I believe that students who have encountered this problem during development will understand what I mean at a glance.
First of all, the Element component has a complete set of form verification methods. The official document is also very clear: Element form verification API. Normally add rules according to the official document. Set props for the form items that need to be verified, and then submit the form. Just validate the form items through the form's validate method.
But here comes the question. If there are form items dynamically generated through v-for, how to set up validation? This official document does not have a clear explanation. By searching for solutions and actual verification, we summarized the solutions as follows.
The first is the code before looping the form item without verification:
<template> <p class="content-body"> <el-form ref="form" :model="form" :rules="rules" label-width="120px"> <el-row :gutter="10"> <el-col :span="12" :offset="0"> <el-form-item label="套餐名称:" prop="activityName" id="activityName"> <el-input v-model="form.activityName"></el-input> </el-form-item> </el-col> </el-row> <el-row :gutter="10"> <el-col :span="12"> <el-form-item label="状态:"> <el-radio v-model="form.status" label="1">上线</el-radio> <el-radio v-model="form.status" label="0">下线</el-radio> </el-form-item> </el-col> </el-row> <el-row :gutter="10"> <el-col :span="2" style="width:120px;"> <p class="sub-title">梯度设置:</p> </el-col> <el-col :span="20"> <el-row :gutter="10" v-for="(item,index) in form.productGroup" :key="index"> <el-col :span="6"> <el-form-item label="商品数量:"> <el-input v-model="item.num" type="number" size="small" style="width:80px;"></el-input> </el-form-item> </el-col> <el-col :span="6"> <el-form-item label="优惠价格:"> <el-input v-model="item.price" type="number" size="small" style="width:80px;"></el-input> </el-form-item> </el-col> <el-col :span="4"> <i class="el-icon-remove-outline" @click="deleteLadder(index)"></i> <i class="el-icon-circle-plus-outline" @click="addLadder" v-if="index==0"></i> </el-col> </el-row> </el-col> </el-row> <el-form-item size="medium" class="p-submit"> <el-button @click="resetForm('form')">取消</el-button> <el-button type="primary" @click="submitForm('form')">提交</el-button> </el-form-item> </el-form> </p> </template> <script> /* eslint-disable */ export default { data() { return { form: { activityName: '', status: '1', productGroup: [{num:"",price:""}] }, rules: { activityName: [ { required: true, message: '请输入套餐名称', trigger: 'blur' } ] } } }, methods: { deleteLadder(index) { if(this.form.productGroup.length>1){ this.form.productGroup.splice(index,1); } }, addLadder() { this.form.productGroup.push({num:"",price:""}); }, submitForm(formName) { console.log("activityName...",this.form.activityName); this.$refs[formName].validate((valid,obj) => { if (valid) { this.submitFormAction(); } else { this.$message.error("验证不通过"); } }); }, submitFormAction() { this.$message.success("提交成功"); }, resetForm(formName) { this.$refs[formName].resetFields(); this.form.productGroup = [{num:"",price:""}]; } } } </script> <style> .el-form-item { margin-bottom: 20px; } </style>
The first is to add rules rules, which is the same as adding normal rules:
productGroupRules: { productGroupNum: [{required: true, message: '请填写商品数量', trigger: 'blur'}], productGroupPrice: [{required: true, message: '请填写优惠价格', trigger: 'blur'}] }
Then add validation to the form item, taking the quantity of the product as an example:
<el-form-item label="商品数量:" :prop="'productGroup.'+index+'.num'" :rules="productGroupRules.productGroupNum"> <el-input v-model="item.num" type="number" size="small" style="width:80px;"></el-input> </el-form-item>
Note here:rules
must be added for each form item. If there are multiple form items, use the form productGroupRules.productGroupNum
to distinguish them, corresponding to the above productGroupRules
content.
The other main thing is :prop
. Note that the normal verification form item is prop
, but here it is :prop
. :prop="'productGroup.' index '.num'"
is in the form of splicing. The front is the array bound by v-for, the middle is the array index index, and the last is the form item binding. the name of the v-model, and then connect them with dot . All three of these must be guaranteed to be correct, and even one wrong one cannot be verified.
Another thing to note is that the array bound to v-for must also be bound to the form object. Pay attention to the data above:
form: { activityName: '', status: '1', productGroup: [{num:"",price:""}] } 如果是: form: { activityName: '', status: '1' }, productGroup: [{num:"",price:""}]
cannot be verified, this should also be noted.
Okay, the above is the solution to the form item verification in the v-for loop when vue uses the Element component. I hope it can help students who encounter this problem.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use vue's transition to complete the sliding transition
About vue and vue-validator form verification function Implementation
The above is the detailed content of About the form item verification method in the v-for loop when vue uses the Element component. For more information, please follow other related articles on the PHP Chinese website!