当前位置:   article > 正文

el-form中数组(一重数组、二重数组以及多重数组)绑定检验的实例(prop以及rules的赋值)_el-form 数组校验

el-form 数组校验
友情提示:如果想需要el-table里面每一行的每个字段都需要进行校验的,请参参考这一下文章

ElementUI el-table动态增减行,通过el-from实现el-table行内输入验证-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/snows_l/article/details/134050576?spm=1001.2014.3001.5502

一、一重数组的绑定
提示:

1、每一项el-form-item的prop以及rules的赋值以及data中form的结构(行内样式是为了复制直接看效果)

2、注意看users以及mobileprop绑定的值不一样的,因此当只有一重数组时这两种写法都是可以的

  1. :prop="'users[' + index + '].userName'"

  1. :prop="'users.' + index + '.mobile'"

结构如下代码所示:
template
  1. <!--
  2. * @Description: ------ 文件描述 ------
  3. * @Creater: snows_l snows_l@163.com
  4. * @Date: 2023-02-12 14:02:00
  5. * @LastEditors: snows_l snows_l@163.com
  6. * @LastEditTime: 2023-02-12 14:24:00
  7. * @FilePath: /vue2-js/src/components/elForm.vue
  8. -->
  9. <template>
  10. <div class="container-warp">
  11. <el-form :model="form" ref="form" :rules="rules" label-width="240px" :inline="false" size="normal">
  12. <el-form-item label="联系人及手机号码">
  13. <div class="name-phone">
  14. <div
  15. style="display: flex; align-items: center"
  16. class="item"
  17. v-for="(item, index) in form.users"
  18. :key="item.id">
  19. <el-form-item :prop="'users[' + index + '].userName'" :rules="rules.userName">
  20. <el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
  21. </el-form-item>
  22. <el-form-item style="margin: 0 10px" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
  23. <el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
  24. </el-form-item>
  25. <i
  26. style="font-size: 20px"
  27. class="icon"
  28. :class="index == 0 ? 'el-icon-circle-plus-outline' : 'el-icon-circle-close'"
  29. @click="addUser(index)"></i>
  30. </div>
  31. </div>
  32. </el-form-item>
  33. <el-form-item>
  34. <el-button type="primary" @click="onSubmit">立即创建</el-button>
  35. <el-button>取消</el-button>
  36. </el-form-item>
  37. </el-form>
  38. </div>
  39. </template>
script
  1. <script>
  2. export default {
  3. name: '',
  4. data() {
  5. return {
  6. form: {
  7. users: [
  8. {
  9. userName: '',
  10. mobile: null
  11. }
  12. ],
  13. otherInfn: null
  14. },
  15. rules: {
  16. userName: [{ required: true, message: '请输入联系人' }],
  17. mobile: [{ required: true, message: '请输入手机号' }]
  18. }
  19. };
  20. },
  21. created() {},
  22. methods: {
  23. onSubmit() {
  24. console.log('-------- form --------', this.form);
  25. this.$refs['form'].validate(valid => {
  26. if (valid) {
  27. alert('submit!');
  28. } else {
  29. console.log('error submit!!');
  30. return false;
  31. }
  32. });
  33. }
  34. }
  35. };
  36. </script>
  37. <style scoped>
  38. .container-warp {
  39. width: 100%;
  40. height: 100%;
  41. }
  42. </style>

二、二重数组的绑定
提示:

1、注意看users以及mobileprop绑定的值不一样的,因此当只有一重数组时这两种写法都是可以的

  1. :prop="'users[' + index + '].list[' + i + '].attr2'"

  1. :prop="'users.' + index + '.list.' + i + '.attr1'"

结构如下代码所示:
template
  1. <template>
  2. <div class="container-warp">
  3. <span style="font-size: 16px; font-weight: 500">form表单的二重数组验证</span>
  4. <el-form :model="form" ref="form" :rules="rules" label-width="240px" label-position="top" :inline="false" size="normal">
  5. <div class="pingpu" style="display: flex; justify-content: space-around">
  6. <el-form-item label="第一层数据1" prop="data1">
  7. <el-input v-model="form.data1" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  8. </el-form-item>
  9. <el-form-item label="第一层数据2" prop="data2">
  10. <el-input v-model="form.data2" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  11. </el-form-item>
  12. <el-form-item label="第一层数据3" prop="data3">
  13. <el-input v-model="form.data3" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  14. </el-form-item>
  15. </div>
  16. <el-form-item label="基础信息users -- 一重数组">
  17. <el-button type="primary" size="small" @click="handleUser">添加一个users</el-button>
  18. <div class="name-phone">
  19. <!-- 第一重数组 -->
  20. <div class="item" v-for="(item, index) in form.users" :key="item.id">
  21. <span>用户{{ index + 1 }}</span>
  22. <div class="user-box" style="margin: 10px 0; padding: 20px; border: 1px solid #999">
  23. <el-form-item :label="'联系人'" label-position="right" :prop="'users[' + index + '].userName'" :rules="rules.userName">
  24. <el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
  25. </el-form-item>
  26. <el-form-item :label="'手机号'" label-position="right" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
  27. <el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
  28. </el-form-item>
  29. <el-button type="primary" size="small" style="margin: 20px 0 10px 0" @click="handleUserOtherInfor(index)">添加用户{{ index + 1 }}其他信息</el-button>
  30. <!-- 第二重数组 -->
  31. <div class="other-info" style="border: 1px solid #eee">
  32. <el-form-item label="其他信息List -- 二重数组" style="margin: 40px">
  33. <div class="other-info-container" style="display: flex" v-for="(single, i) in form.users[index].list" :key="i">
  34. <el-form-item style="margin-right: 20px" label="其他信息1" size="normal" :prop="'users.' + index + '.list.' + i + '.attr1'" :rules="rules.attr1">
  35. <el-input v-model="single.attr1" size="small" placeholder="请输入其他信息1" />
  36. </el-form-item>
  37. <el-form-item label="其他信息2" size="normal" :prop="'users[' + index + '].list[' + i + '].attr2'" :rules="rules.attr2">
  38. <el-input v-model="single.attr2" size="small" placeholder="请输入其他信息2" />
  39. </el-form-item>
  40. </div>
  41. </el-form-item>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </el-form-item>
  47. <el-form-item>
  48. <el-button type="primary" size="small" @click="onSubmit">立即创建</el-button>
  49. <el-button size="small">取消</el-button>
  50. </el-form-item>
  51. </el-form>
  52. </div>
  53. </template>
script
  1. <script>
  2. export default {
  3. name: '',
  4. data() {
  5. return {
  6. form: {
  7. data1: '',
  8. data2: '',
  9. data3: '',
  10. users: [
  11. {
  12. userName: '',
  13. mobile: null,
  14. otherInfn: null,
  15. list: [{ attr1: null, attr2: null }]
  16. }
  17. ],
  18. otherInfn: null
  19. },
  20. rules: {
  21. data1: [{ required: true, message: '请输入第一层数据1' }],
  22. data2: [{ required: true, message: '请输入第一层数据2' }],
  23. data3: [{ required: true, message: '请输入第一层数据3' }],
  24. userName: [{ required: true, message: '请输入联系人' }],
  25. mobile: [{ required: true, message: '请输入手机号' }],
  26. attr1: [{ required: true, message: '请输入其他信息1' }],
  27. attr2: [{ required: true, message: '请输入其他信息2' }]
  28. }
  29. };
  30. },
  31. created() {},
  32. methods: {
  33. onSubmit() {
  34. console.log('-------- form --------', this.form);
  35. this.$refs['form'].validate(valid => {
  36. if (valid) {
  37. alert('submit!');
  38. } else {
  39. console.log('error submit!!');
  40. return false;
  41. }
  42. });
  43. },
  44. handleUser() {
  45. this.form.users.push({
  46. userName: '',
  47. mobile: null,
  48. otherInfn: null,
  49. list: [
  50. { attr1: null, attr2: null },
  51. { attr1: null, attr2: null }
  52. ]
  53. });
  54. },
  55. handleUserOtherInfor(index) {
  56. this.form.users[index].list.push({ attr1: null, attr2: null });
  57. },
  58. changeData(e) {
  59. console.log('-------- e --------', e);
  60. }
  61. }
  62. };
  63. </script>

三、效果如如下:(二重数组的效果,就随便弄了下不要在乎样式[/手动狗头])

图1:初始效果

图2:校验的效果

图3:添加用户 -> 添加用户其他信息的效果

三、二重数组vue完整文件(二重数组也包含了一重数组的)

  1. <template>
  2. <div class="container-warp">
  3. <span style="font-size: 16px; font-weight: 500">form表单的二重数组验证</span>
  4. <el-form :model="form" ref="form" :rules="rules" label-width="240px" label-position="top" :inline="false" size="normal">
  5. <div class="pingpu" style="display: flex; justify-content: space-around">
  6. <el-form-item label="第一层数据1" prop="data1">
  7. <el-input v-model="form.data1" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  8. </el-form-item>
  9. <el-form-item label="第一层数据2" prop="data2">
  10. <el-input v-model="form.data2" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  11. </el-form-item>
  12. <el-form-item label="第一层数据3" prop="data3">
  13. <el-input v-model="form.data3" @input="e => e.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" placeholder="请输入" size="normal"></el-input>
  14. </el-form-item>
  15. </div>
  16. <el-form-item label="基础信息users -- 一重数组">
  17. <el-button type="primary" size="small" @click="handleUser">添加一个users</el-button>
  18. <div class="name-phone">
  19. <!-- 第一重数组 -->
  20. <div class="item" v-for="(item, index) in form.users" :key="item.id">
  21. <span>用户{{ index + 1 }}</span>
  22. <div class="user-box" style="margin: 10px 0; padding: 20px; border: 1px solid #999">
  23. <el-form-item :label="'联系人'" label-position="right" :prop="'users[' + index + '].userName'" :rules="rules.userName">
  24. <el-input v-model="item.userName" size="small" placeholder="请输入联系人" />
  25. </el-form-item>
  26. <el-form-item :label="'手机号'" label-position="right" :prop="'users.' + index + '.mobile'" :rules="rules.mobile">
  27. <el-input v-model="item.mobile" size="small" placeholder="请输入手机号码" />
  28. </el-form-item>
  29. <el-button type="primary" size="small" style="margin: 20px 0 10px 0" @click="handleUserOtherInfor(index)">添加用户{{ index + 1 }}其他信息</el-button>
  30. <!-- 第二重数组 -->
  31. <div class="other-info" style="border: 1px solid #eee">
  32. <el-form-item label="其他信息List -- 二重数组" style="margin: 40px">
  33. <div class="other-info-container" style="display: flex" v-for="(single, i) in form.users[index].list" :key="i">
  34. <el-form-item style="margin-right: 20px" label="其他信息1" size="normal" :prop="'users.' + index + '.list.' + i + '.attr1'" :rules="rules.attr1">
  35. <el-input v-model="single.attr1" size="small" placeholder="请输入其他信息1" />
  36. </el-form-item>
  37. <el-form-item label="其他信息2" size="normal" :prop="'users[' + index + '].list[' + i + '].attr2'" :rules="rules.attr2">
  38. <el-input v-model="single.attr2" size="small" placeholder="请输入其他信息2" />
  39. </el-form-item>
  40. </div>
  41. </el-form-item>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </el-form-item>
  47. <el-form-item>
  48. <el-button type="primary" size="small" @click="onSubmit">立即创建</el-button>
  49. <el-button size="small">取消</el-button>
  50. </el-form-item>
  51. </el-form>
  52. </div>
  53. </template>
  54. <script>
  55. export default {
  56. name: '',
  57. data() {
  58. return {
  59. form: {
  60. data1: '',
  61. data2: '',
  62. data3: '',
  63. users: [
  64. {
  65. userName: '',
  66. mobile: null,
  67. otherInfn: null,
  68. list: [{ attr1: null, attr2: null }]
  69. }
  70. ],
  71. otherInfn: null
  72. },
  73. rules: {
  74. data1: [{ required: true, message: '请输入第一层数据1' }],
  75. data2: [{ required: true, message: '请输入第一层数据2' }],
  76. data3: [{ required: true, message: '请输入第一层数据3' }],
  77. userName: [{ required: true, message: '请输入联系人' }],
  78. mobile: [{ required: true, message: '请输入手机号' }],
  79. attr1: [{ required: true, message: '请输入其他信息1' }],
  80. attr2: [{ required: true, message: '请输入其他信息2' }]
  81. }
  82. };
  83. },
  84. created() {},
  85. methods: {
  86. onSubmit() {
  87. console.log('-------- form --------', this.form);
  88. this.$refs['form'].validate(valid => {
  89. if (valid) {
  90. alert('submit!');
  91. } else {
  92. console.log('error submit!!');
  93. return false;
  94. }
  95. });
  96. },
  97. handleUser() {
  98. this.form.users.push({
  99. userName: '',
  100. mobile: null,
  101. otherInfn: null,
  102. list: [
  103. { attr1: null, attr2: null },
  104. { attr1: null, attr2: null }
  105. ]
  106. });
  107. },
  108. handleUserOtherInfor(index) {
  109. this.form.users[index].list.push({ attr1: null, attr2: null });
  110. },
  111. changeData(e) {
  112. console.log('-------- e --------', e);
  113. }
  114. }
  115. };
  116. </script>
  117. <style scoped>
  118. .container-warp {
  119. padding: 20px;
  120. }
  121. .el-input {
  122. max-width: 400px;
  123. }
  124. </style>
四、总结:

prop的绑定就有上文提到的这两种方式,因此不管是几重数组都是可以的(当然没有试过三重四重数组, 应该是可以的,当然项目中应该不会出现这种情况)

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

闽ICP备14008679号