Home > Web Front-end > JS Tutorial > body text

About the form item verification method in the v-for loop when vue uses the Element component

不言
Release: 2018-06-29 11:17:50
Original
3427 people have browsed it

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(&#39;form&#39;)">取消</el-button>
    <el-button type="primary" @click="submitForm(&#39;form&#39;)">提交</el-button>
   </el-form-item>
  </el-form>
 </p>
</template>
<script>
/* eslint-disable */
export default {
 data() {
  return {
   form: {
    activityName: &#39;&#39;,
    status: &#39;1&#39;,
    productGroup: [{num:"",price:""}]
   },
   rules: {
    activityName: [
     { required: true, message: &#39;请输入套餐名称&#39;, trigger: &#39;blur&#39; }
    ]
   }
  }
 },
 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>
Copy after login

The first is to add rules rules, which is the same as adding normal rules:

productGroupRules: {
 productGroupNum: [{required: true, message: &#39;请填写商品数量&#39;, trigger: &#39;blur&#39;}],
 productGroupPrice: [{required: true, message: &#39;请填写优惠价格&#39;, trigger: &#39;blur&#39;}]
}
Copy after login

Then add validation to the form item, taking the quantity of the product as an example:

<el-form-item label="商品数量:" :prop="&#39;productGroup.&#39;+index+&#39;.num&#39;" :rules="productGroupRules.productGroupNum">
 <el-input v-model="item.num" type="number" size="small" style="width:80px;"></el-input>
</el-form-item>
Copy after login

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: &#39;&#39;,
 status: &#39;1&#39;,
 productGroup: [{num:"",price:""}]
}
如果是:

form: {
 activityName: &#39;&#39;,
 status: &#39;1&#39;
},
productGroup: [{num:"",price:""}]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template