当前位置:   article > 正文

el-form提交时校验循环出来的多个form表单_el-form循环表单验证

el-form循环表单验证

1. 需求

循环得到的表单     在点击按钮时要验证每一个form

 

 

 2.解决方法

  1. <template>
  2. <div>
  3. <div class="formBox" v-for="(item, index) in ruleForm" :key="index" style="border:1px solid #ccc;margin-top: 20px;">
  4. <el-form :model="ruleForm[index]" :rules="rules" :ref="'ruleFormRef'+index" label-width="100px">
  5. <el-form-item label="活动名称" prop="name">
  6. <el-input v-model="item.name"></el-input>
  7. </el-form-item>
  8. <el-form-item label="活动区域" prop="region">
  9. <el-select v-model="item.region" placeholder="请选择活动区域">
  10. <el-option label="区域一" value="shanghai"></el-option>
  11. <el-option label="区域二" value="beijing"></el-option>
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item label="活动形式" prop="desc">
  15. <el-input type="textarea" v-model="item.desc"></el-input>
  16. </el-form-item>
  17. </el-form>
  18. </div>
  19. <el-button type="primary" @click="submitForm('ruleFormRef')">立即创建</el-button>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. ruleFormRef: {
  27. },
  28. ruleForm: [{
  29. name: '',
  30. region: '',
  31. desc: '',
  32. }, {
  33. name: '',
  34. region: '',
  35. desc: '',
  36. }, {
  37. name: '',
  38. region: '',
  39. desc: '',
  40. }],
  41. rules: {
  42. name: [
  43. { required: true, message: '请输入活动名称', trigger: 'blur' },
  44. ],
  45. region: [
  46. { required: true, message: '请选择活动区域', trigger: 'change' }
  47. ],
  48. desc: [
  49. { required: true, message: '请填写活动形式', trigger: 'blur' }
  50. ]
  51. }
  52. };
  53. },
  54. methods: {
  55. submitForm(formName) {
  56. const falg = []
  57. console.log(formName);
  58. for (let index = 0; index < this.ruleForm.length; index++) {
  59. console.log(this.$refs[formName + index]);
  60. this.$refs[formName + index][0].validate((valid) => {
  61. if (valid) {
  62. falg.push(1)
  63. }
  64. });
  65. }
  66. if (falg.length == this.ruleForm.length) {
  67. alert(1)
  68. } else {
  69. alert('校验失败')
  70. }
  71. },
  72. }
  73. }
  74. </script>

3.效果图

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/115719?site
推荐阅读
相关标签
  

闽ICP备14008679号