当前位置:   article > 正文

el-form循环el-form-item多个类型input并进行动态验证_el-form-item循环

el-form-item循环
  1. <el-form
  2. ref="ruleForm"
  3. :model="ruleForm"
  4. :rules="rules"
  5. hide-required-asterisk
  6. :label-position="labelPosition"
  7. label-width="70px"
  8. >
  9. <el-form-item
  10. v-for="(labelItem, indexs) in ruleForm.formList"
  11. :key="indexs"
  12. :label="title == '修改类型名称' ? '类型名称' : labelItem.name"
  13. :prop="'formList.' + indexs + '.value'"
  14. :rules="[
  15. {
  16. required: labelItem.must,
  17. message: '请输入' + labelItem.name,
  18. trigger: ['blur'],
  19. },
  20. {
  21. validator:
  22. labelItem.type == 3
  23. ? checkPhone
  24. : labelItem.type == 4
  25. ? checkEmil
  26. :labelItem.type == 5
  27. ? checkNum
  28. : '',
  29. trigger: ['blur'],
  30. },
  31. ]"
  32. >
  33. <el-input
  34. v-if="labelItem.type != 2"
  35. class="label-input"
  36. :type="
  37. labelItem.type == 3 || labelItem.type == 5 ? 'number' : 'text'
  38. "
  39. :placeholder="
  40. title == '修改类型名称'
  41. ? '请输入类型名称'
  42. : '请输入' + labelItem.name
  43. "
  44. v-model="labelItem.value"
  45. autocomplete="off"
  46. >
  47. </el-input>
  48. <el-date-picker
  49. v-if="labelItem.type == 2"
  50. style="width: 100%"
  51. v-model="labelItem.value"
  52. type="datetime"
  53. format="yyyy-MM-dd HH:mm:ss"
  54. value-format="yyyy-MM-dd HH:mm:ss"
  55. placeholder="选择到期时间"
  56. default-time="12:00:00"
  57. >
  58. </el-date-picker>
  59. </el-form-item>
  60. </el-form>
  61. <div slot="footer" class="dialog-footer">
  62. <el-button
  63. class="btn"
  64. type="primary"
  65. @click="submitForm('ruleForm', index, item)">提交</el-button>
  66. <el-button class="btn" @click="closeForm('ruleForm', index, item)">取消</el-button>
  67. </div>
  1. data(){
  2. return{
  3. title:'添加卡片',
  4. ruleForm: {
  5. name: "",
  6. tagClassifyId: "",
  7. id: null,
  8. formList: [
  9. {
  10. "disable": true,
  11. "name": "卡片名称",
  12. "must": true,
  13. "id": "",
  14. "type": 1,
  15. "value": "12"
  16. },
  17. {
  18. "disable": false,
  19. "name": "电话",
  20. "must": true,
  21. "id": "",
  22. "type": 1,
  23. "value": ""
  24. },
  25. {
  26. "disable": false,
  27. "name": "邮箱",
  28. "must": false,
  29. "id": "",
  30. "type": 1,
  31. "value": ""
  32. }
  33. ],
  34. },
  35. }
  36. },
  37. methods: {
  38. checkPhone(rule, value, callback) {
  39. if (!value || vaildPhone(value)) return callback();
  40. return callback(new Error("请输入正确的格式"));
  41. },
  42. checkEmil(rule, value, callback) {
  43. if (!value || validMailbox(value)) return callback();
  44. return callback(new Error("请输入正确的格式"));
  45. },
  46. checkNum(rule, value, callback){
  47. if(!value || validNum(value)) return callback();
  48. return callback(new Error("请输入正确的格式"));
  49. },
  50. submitForm(ruleForm, index, item) {
  51. this.$refs[ruleForm][index].validate(async (valid) => {
  52. if (valid) {
  53. createLabel({
  54. tagClassifyId: item.id,
  55. formList: item.filedType,
  56. }).then((res) => {
  57. if (res.code == 0) {
  58. this.$modal.msgSuccess("添加成功");
  59. item.addFlag = false;
  60. this.getTagListByName(this.labelVal);
  61. } else {
  62. this.$modal.msgWarning("添加失败");
  63. }
  64. });
  65. Object.assign(this.$data.ruleForm, this.$options.data().ruleForm);
  66. }
  67. });
  68. },
  69. closeForm(ruleForm, index, item) {
  70. this.$refs[ruleForm][index].resetFields();
  71. item.addFlag = false;
  72. },
  73. }

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

闽ICP备14008679号