当前位置:   article > 正文

解决ElementUI动态表单校验验证不通过_动态生成的表单el-select校验老是出问题

动态生成的表单el-select校验老是出问题

这里记录一下,写项目时遇到的一个问题:就是动态渲染的表单项,加验证规则后一直不通过!!!

原代码

html部分:

  1. <el-form-item
  2. v-for="(teaclass,index) in addFom.classIds"
  3. :label="`班级${index+1}`"
  4. prop="classId"
  5. >
  6. <el-row :gutter="12">
  7. <el-col :span="12">
  8. <el-select v-model="teaclass.classId" placeholder="请选择">
  9. <el-option
  10. v-for="(item) in filterOptions[index]"
  11. :label="item.className"
  12. :value="item.classId"
  13. :key="item.classId"
  14. />
  15. </el-select>
  16. </el-col>
  17. <el-col :span="5">
  18. <el-button type="danger" icon="el-icon-delete" circle @click="removeClassId(index)"/>
  19. </el-col>
  20. </el-row>
  21. </el-form-item>
  22. <el-form-item>
  23. <el-button type="success" @click="submitTake('ruleForm')">提交</el-button>
  24. <el-button @click="addClassId" :disabled="disabled">添加班级</el-button>
  25. </el-form-item>

 js部分:

  1. return {
  2. tableData: [],
  3. teaName: '',
  4. page: 1,
  5. total: 0,
  6. pageSize: 12,
  7. classData: [],
  8. show: false,
  9. dra: false,
  10. direction: 'rtl',
  11. addFom: {
  12. teaName: '',
  13. teaSex: true,
  14. teaType: true,
  15. teaPhone: '',
  16. seniority: 0,
  17. classIds: [{}]
  18. },
  19. classAll: [],
  20. disabled: false,
  21. rules: {
  22. teaName: [{ required: true, message: '请输入教师名称', trigger: 'blur' }],
  23. teaSex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  24. teaType: [{ required: true, message: '请选择类型', trigger: 'blur' }],
  25. teaPhone: [
  26. { required: true, message: '请输入联系电话', trigger: 'blur' },
  27. { min: 11, max: 11, message: '请输入长度11位的大陆号码', trigger: 'blur' }
  28. ],
  29. seniority: [{ required: true, message: '请输入教龄', trigger: 'blur' }],
  30. classId: [{ required: true, message: '请选择班级', trigger: 'change' }]
  31. }
  32. }

结果: 

 可以看到,按照官网的写法动态渲染的表单项是无法进行校验验证的。这里就是无论选中没选中值都校验不通过。

解决

修改prop、rules

  1. <el-form-item
  2. v-for="(teaclass,index) in addFom.classIds"
  3. :label="`班级${index+1}`"
  4. :prop="'classIds.'+ index + '.classId'"
  5. :rules="rules.NotEmpty"
  6. >
  7. <el-row :gutter="12">
  8. <el-col :span="12">
  9. <el-select v-model="teaclass.classId" placeholder="请选择">
  10. <el-option
  11. v-for="(item) in filterOptions[index]"
  12. :label="item.className"
  13. :value="item.classId"
  14. :key="item.classId"
  15. />
  16. </el-select>
  17. </el-col>
  18. <el-col :span="5">
  19. <el-button type="danger" icon="el-icon-delete" circle @click="removeClassId(index)"/>
  20. </el-col>
  21. </el-row>
  22. </el-form-item>
  1. rules: {
  2. teaName: [{ required: true, message: '请输入教师名称', trigger: 'blur' }],
  3. teaSex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  4. teaType: [{ required: true, message: '请选择类型', trigger: 'blur' }],
  5. teaPhone: [
  6. { required: true, message: '请输入联系电话', trigger: 'blur' },
  7. { min: 11, max: 11, message: '请输入长度11位的大陆号码', trigger: 'blur' }
  8. ],
  9. seniority: [{ required: true, message: '请输入教龄', trigger: 'blur' }],
  10. NotEmpty: [{ required: true, message: '请选择班级', trigger: 'change' }]
  11. }

结果:

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