当前位置:   article > 正文

element-ui el-form 动态数据循环表单验证的方法_element-ui中的form循环进行动态校验

element-ui中的form循环进行动态校验
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.1/theme-chalk/index.min.css" />
  9. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js"></script>
  10. <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.1/index.min.js"></script>
  11. </head>
  12. <body>
  13. <div id="app" style="width: 800px; height: 400px">
  14. <!-- 整个表单 -->
  15. <el-form :model="formInfo" size="mini" :rules="rules" ref="formInfo" inline>
  16. <!-- 固定值 公共部分-->
  17. <el-form-item label="名称" prop="name">
  18. <el-input v-model="formInfo.name" size="mini" />
  19. </el-form-item>
  20. <!-- 循环值 prop为'userList.'+index+'.name' .每一个循环中的<el-form-item>都需要加:rules-->
  21. <div v-for="(item, index) in formInfo.list" :key="index" class="inline">
  22. <el-form-item label="账号:" :prop="'list.'+ index +'.username'" :rules="rules.username">
  23. <el-input v-model="item.username" size="mini" />
  24. </el-form-item>
  25. <el-form-item label="密码:" :prop="'list.'+ index +'.password'" :rules="rules.password">
  26. <el-input v-model="item.password" type="password" />
  27. </el-form-item>
  28. <el-form-item label="手机号:" :prop="'list.'+ index +'.phone'" :rules="rules.phone">
  29. <el-input v-model="item.phone" size="mini" />
  30. </el-form-item>
  31. </div>
  32. </el-form>
  33. <el-button type="primary" @click="add">添加</el-button>
  34. <el-button type="primary" @click="submit">提交</el-button>
  35. </div>
  36. <script>
  37. const lx = new Vue({
  38. el: '#app',
  39. data() {
  40. return {
  41. rules: {
  42. name: [
  43. {
  44. required: true,
  45. message: '请输入名称',
  46. trigger: 'blur',
  47. },
  48. ],
  49. phone: [
  50. {
  51. required: true,
  52. message: '请输入账号手机号',
  53. trigger: 'blur',
  54. },
  55. ],
  56. username: [
  57. {
  58. required: true,
  59. message: '请输入账号名称',
  60. trigger: 'blur',
  61. },
  62. ],
  63. password: [
  64. {
  65. required: true,
  66. message: '请输入密码',
  67. trigger: 'blur',
  68. },
  69. ],
  70. }, // 添加校验
  71. formInfo: {
  72. name: '',
  73. list: [
  74. {
  75. phone: '',
  76. username: '',
  77. password: '',
  78. },
  79. ],
  80. },
  81. }
  82. },
  83. watch: {},
  84. computed: {},
  85. methods: {
  86. // 新增
  87. add() {
  88. this.formInfo.list.push({ phone: '', username: '', password: '' })
  89. },
  90. // 提交
  91. submit() {
  92. let isValid = false
  93. this.$refs.formInfo.validate((valid) => {
  94. if (valid) isValid = true
  95. })
  96. if (!isValid) return
  97. },
  98. },
  99. created() {},
  100. })
  101. </script>
  102. <style>
  103. .inline {
  104. display: flex;
  105. }
  106. </style>
  107. </body>
  108. </html>

 

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