当前位置:   article > 正文

在element动态增加表单校验时,增加第二个校验总是不通过解决方式_rules.push({ required: true, validator: only, trig

rules.push({ required: true, validator: only, trigger: 'blur' })

我遇到了一个问题,在动态增加表单校验时,增加第二个校验总是不通过,结果发现,我增加的mobileRules为一维数组,当我push进去对应对象事,就会在一个数组里面增加多个校验方式,而每次从第一个开始校验,第一个没通过就直接return。

错误写法(mobileRules等于push进去的所有方法,而不是单一的校验方式):

  1. <el-form-item
  2. v-for="(contact, index) in form.contacts"
  3. :key="contact.key"
  4. :prop="'contacts.' + index + '.value'"
  5. :rules="mobileRules">
  6. </el-form-item>
  1. this.mobileRules.push({
  2. required: true,
  3. validator: this.$validate.internationalPhone,
  4. trigger: 'blur'
  5. });

下面来看修改之后的代码(用二维数组

首先,校验我写了一个全局js(validate.js),加到全局链上

  1. function validatePhone(rule, value, callback, type) {
  2. let regs = {
  3. phoneDL: /^(\+86|86)?1[0-9]{10}$/,
  4. phoneHK: /^(5|6|8|9)\d{7}$/, // 八位数,5689开头
  5. phoneTW: /^09\d{8}$/, // 十位数,09开头
  6. mail: /^[\.A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  7. };
  8. let reg = regs[type];
  9. let textNull = type == 'mail' ? '请输入邮箱' : '请输入手机号';
  10. let textErr = type == 'mail' ? '请输入正确的邮箱' : '请输入正确的手机号码';
  11. if (!value) {
  12. callback(new Error(textNull));
  13. } else {
  14. if (!reg.test(value)) {
  15. callback(new Error(textErr));
  16. }
  17. callback();
  18. }
  19. };
  20. class Validate {
  21. /**
  22. * 校验手机号
  23. */
  24. static internationalPhone(rule, value, callback) {
  25. validatePhone(rule, value, callback, 'phoneDL');
  26. };
  27. static internationalTWPhone(rule, value, callback) {
  28. validatePhone(rule, value, callback, 'phoneTW');
  29. };
  30. static internationalHKPhone(rule, value, callback) {
  31. validatePhone(rule, value, callback, 'phoneHK');
  32. };
  33. static internationalMail(rule, value, callback) {
  34. validatePhone(rule, value, callback, 'mail');
  35. };
  36. }
  37. export default Validate;

加入到main.js(全局可以用$validate调用到里面的方法)

  1. import validate from '@/assets/utils/validate.js';
  2. Vue.prototype.$validate = validate;

点击动态添加表单项:

  1. <el-form ref="form" :model="form">
  2. <el-form-item
  3. v-for="(contact, index) in form.contacts"
  4. :key="contact.key"
  5. :prop="'contacts.' + index + '.value'"
  6. :rules="mobileRules[index]"
  7. >
  8. <div class="international-mobile">
  9. <international-mobile
  10. style="width:80px;"
  11. v-if="!contact.contactType"
  12. @updateInternationalTelephone="updateInternationalTelephone(index)"
  13. :internationalTelephone.sync="contact.internationalTelephone">
  14. </international-mobile>
  15. <el-input v-model.trim="contact.value" class="international-telephone"
  16. ref='newPhoneMobile' @keydown.native="prevent($event)"
  17. :placeholder="contact.contactType? '请输入邮箱地址': '请输入手机号'"></el-input>
  18. <span class="pointer-text light-text fr" @click="changeContactType(index)">{{
  19. contact.contactType? '填写手机号' : '填写邮箱' }}</span>
  20. </div>
  21. <el-button
  22. slot="append"
  23. icon="minus"
  24. @click="deleteContact(contact)"
  25. v-if="form.contacts.length>1"></el-button>
  26. /el-form-item>
  27. </el-form>
  28. <div class="addContact" @click="addContact">
  29. <i class="icon-add"></i>
  30. <span>添加联系方式</span>
  31. </div>
  1. <script>
  2. export default {
  3. data() {
  4. return {
  5. form: {
  6. contacts: [{
  7. value: '',
  8. contactType: false,
  9. internationalTelephone: '86'
  10. }],
  11. },
  12. // 此处设置成二维数组,以便每个验证对应自己的数组
  13. mobileRules: [[{
  14. required: true,
  15. validator: this.$validate.internationalPhone,
  16. trigger: 'blur'
  17. }]]
  18. }
  19. },
  20. methods: {
  21. updateInternationalTelephone(index) {
  22. if (this.form.contacts[index].internationalTelephone == '86') {
  23. this.mobileRules[index][0].validator = this.$validate.internationalPhone;
  24. } else if (this.form.contacts[index].internationalTelephone == '852') {
  25. this.mobileRules[index][0].validator =
  26. this.$validate.internationalHKPhone;
  27. } else if (this.form.contacts[index].internationalTelephone == '886'){
  28. this.mobileRules[index][0].validator =
  29. this.$validate.internationalTWPhone;
  30. }
  31. // 获取焦点-以便重新验证
  32. this.$refs['newPhoneMobile'][index].$el.children[0].focus();
  33. },
  34. changeContactType(index) {
  35. this.form.contacts[index].contactType =
  36. !this.form.contacts[index].contactType;
  37. if (this.form.contacts[index].contactType) {
  38. this.mobileRules[index].validator = this.$validate.internationalMail;
  39. } else {
  40. if (this.form.contacts[index].internationalTelephone == '86') {
  41. this.mobileRules[index][0].validator =
  42. this.$validate.internationalPhone;
  43. } else if (this.form.contacts[index].internationalTelephone == '852') {
  44. this.mobileRules[index][0].validator =
  45. this.$validate.internationalHKPhone;
  46. } else if (this.form.contacts[index].internationalTelephone == '886'){
  47. this.mobileRules[index][0].validator =
  48. this.$validate.internationalTWPhone;
  49. }
  50. }
  51. // 获取焦点-以便重新验证
  52. this.$refs['newPhoneMobile'][index].$el.children[0].focus();
  53. },
  54. //增加联系方式
  55. addContact() {
  56. this.$refs['form'].validate((valid) => {
  57. if (valid) {
  58. this.form.contacts.push({
  59. value: '',
  60. key: Date.now(),
  61. contactType: false,
  62. internationalTelephone: '86'
  63. });
  64. this.mobileRules.push([{
  65. required: true,
  66. validator: this.$validate.internationalPhone,
  67. trigger: 'blur'
  68. }]);
  69. this.$nextTick(() => {
  70. this.$refs.popover.updatePopper()
  71. })
  72. } else {
  73. }
  74. });
  75. },
  76. //删除联系方式
  77. deleteContact(item) {
  78. var index = this.form.contacts.indexOf(item)
  79. if (index !== -1) {
  80. this.form.contacts.splice(index, 1)
  81. }
  82. this.$nextTick(() => {
  83. this.$refs.popover.updatePopper()
  84. })
  85. },
  86. }
  87. }
  88. </script>

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

闽ICP备14008679号