• elementUI elfrom表单验证无效、不起作用常见原因


    今天遇到一个变态的问题,因页面比较复杂,出现几组条件判断,每个template内部又包含很多表单!!

    1. <template v-else-if="transformTypeValue == 2">template>
    2. <template v-else-if="transformTypeValue == 3">template>
    3. <template v-else-if="transformTypeValue == 4">template>

    竟然发现表单的验证规则只有某一个无效了!!!其他一切正常!!太变态了!!

    先分析以下代码:

    1. <template v-if="transformTypeValue == 4">
    2. <el-row>
    3. <el-col :span="10">
    4. <el-form-item label="入门费(元)" class="is-required" prop="enterFee">
    5. <el-input style="width:100%" v-model="forms.enterFee" placeholder="请输入入门费">el-input>
    6. el-form-item>
    7. el-col>
    8. <el-col :span="10">
    9. <el-form-item label="提成方式">
    10. <el-select style="width:100%" v-model="forms.commissionType" placeholder="提成方式">
    11. <el-option v-for="a in dict.type.commission_options" :key="a.value" :label="a.label"
    12. :value="a.value">el-option>
    13. el-select>
    14. el-form-item>
    15. el-col>
    16. el-row>
    17. <el-row>
    18. <el-col :span="10">
    19. <el-form-item label="时间段" class="is-required" prop="licenseDate">
    20. <el-date-picker style="width:100%" v-model="forms.licenseDate" type="daterange" format="yyyy-MM-dd"
    21. start-placeholder="开始日期" range-separator="-" end-placeholder="结束日期"
    22. placeholder="时间段">el-date-picker>
    23. el-form-item>
    24. el-col>
    25. <el-col :span="10">
    26. <el-form-item label="收取比例(%)" class="is-required" prop="colloctionRatio">
    27. <el-input style="width:100%" v-model="forms.colloctionRatio" placeholder="请输入收取比例">el-input>
    28. el-form-item>
    29. el-col>
    30. el-row>
    31. <el-row>
    32. <el-col :span="10">
    33. <el-form-item label="受让单位" prop="transform2Units" class="is-required">
    34. <el-input style="width:100%" v-model="forms.transform2Units" placeholder="请输入受让单位">el-input>
    35. el-form-item>
    36. el-col>
    37. <el-col :span="10">
    38. <el-form-item label="通讯地址" prop="contactAddress" class="is-required">
    39. <el-input style="width:100%" v-model="forms.contactAddress" placeholder="请输入通讯地址">el-input>
    40. el-form-item>
    41. el-col>
    42. el-row>
    43. <el-row>
    44. <el-col :span="10">
    45. <el-form-item label="联系人" prop="contactUser" class="is-required">
    46. <el-input style="width:100%" v-model="forms.contactUser" placeholder="请输入联系人">el-input>
    47. el-form-item>
    48. el-col>
    49. <el-col :span="10">
    50. <el-form-item label="联系人联系方式" prop="contactUserPhone" class="is-required">
    51. <el-input style="width:100%" v-model="forms.contactUserPhone" placeholder="请输入联系人联系方式">el-input>
    52. el-form-item>
    53. el-col>
    54. el-row>
    55. <el-row>
    56. <el-col :span="10">
    57. <el-form-item label="合同生效日期" prop="bargainDate" class="is-required">
    58. <el-date-picker style="width:100%" v-model="forms.bargainDate" placeholder="请选择合同生效日期" format="yyyy-MM-dd"
    59. type="daterange" range-separator="至" start-placeholder="开始日期"
    60. end-placeholder="结束日期">el-date-picker>
    61. el-form-item>
    62. el-col>
    63. el-row>
    64. template>

    最外层使用el-form,第二层使用el-form-item,第三层使用el-input

    el-form绑定的数据对象为forms,表单的验证规则定义在formRules。整体代码如下:

    1. data() {
    2. return {
    3. forms: {
    4. outcomesName: null,
    5. outcomesType: null,
    6. transformExpenditure: null,
    7. outcomesTransformTime: null,
    8. achievementUnit: null,
    9. achievementDeptId: null,
    10. isFundUpport: '1',
    11. transformExpenditure: null,
    12. transformPathway: null,
    13. transformType: null,
    14. attachment: [],
    15. totalInvestment: null,
    16. projInformation: null,
    17. transformMoney: null,
    18. transform2Units: null,
    19. contactAddress: null,
    20. contactUser: null,
    21. contactUserPhone: null,
    22. bargainDate: null,
    23. enterFee: null,
    24. commissionType: null,
    25. licenseDate: null,
    26. colloctionRatio: null,
    27. stockPropotion:null,
    28. cooperationUnit:null,
    29. },
    30. formRules: {
    31. outcomesName: [
    32. { required: true, message: "成果名称不能为空", trigger: ["blur", "change"] }
    33. ],
    34. outcomesType: [
    35. { required: true, message: "成果类型不能为空", trigger: ["blur", "change"] }
    36. ],
    37. transformExpenditure: [
    38. { required: true, message: "转化经费不能为空", trigger: ["blur", "change"] }
    39. ],
    40. outcomesTransformTime: [
    41. { required: true, message: "成果转化时间不能为空", trigger: ["blur", "change"] }
    42. ],
    43. achievementUnit: [
    44. { required: true, message: "成果所属单位不能为空", trigger: ["blur", "change"] }
    45. ],
    46. achievementDeptId: [
    47. { required: true, message: "成果归属部门不能为空", trigger: ["blur", "change"] }
    48. ],
    49. transformPathway: [
    50. { required: true, message: "转化途径不能为空", trigger: ["blur", "change"] }
    51. ],
    52. totalInvestment: [
    53. { required: true, message: "总投资金额不能为空", trigger: ["blur", "change"] }
    54. ],
    55. transformMoney: [
    56. { required: true, message: "转让金额不能为空", trigger: ["blur", "change"] }
    57. ],
    58. transform2Units: [
    59. { required: true, message: "受让单位不能为空", trigger: ["blur", "change"] }
    60. ],
    61. contactAddress: [
    62. { required: true, message: "通讯地址不能为空", trigger: ["blur", "change"] }
    63. ],
    64. contactUser: [
    65. { required: true, message: "联系人不能为空", trigger: ["blur", "change"] }
    66. ],
    67. contactUserPhone: [
    68. { required: true, message: "联系人联系方式不能为空", trigger: ["blur", "change"] }
    69. ],
    70. bargainDate: [
    71. { required: true, message: "合同生效日期不能为空", trigger: ["blur", "change"] }
    72. ],
    73. enterFee: [
    74. { required: true, message: "入门费不能为空", trigger: ["blur", "change"] }
    75. ],
    76. commissionType: [
    77. { required: true, message: "提成方式不能为空", trigger: ["blur", "change"] }
    78. ],
    79. licenseDate: [
    80. { required: true, message: "时间段不能为空", trigger: ["blur", "change"] }
    81. ],
    82. colloctionRatio: [
    83. { required: true, message: "收取比例不能为空", trigger: ["blur", "change"] }
    84. ],
    85. stockPropotion: [
    86. { required: true, message: "占股比例不能为空", trigger: ["blur", "change"] }
    87. ],
    88. cooperationUnit: [
    89. { required: true, message: "合作单位不能为空", trigger: ["blur", "change"] }
    90. ],
    91. },
    92. };
    93. },

     再来看看触发验证时,通讯地址一栏无论如何也无法触发验证!!!如下动画!!

     通讯地址的验证表单prop='contactAddress',表单内的input也是forms.contactAddress,验证规则写的也是contactAddress,这完全没有问题呀,可是为啥呢?为啥呢?为啥呢?为啥呢?为啥呢?为啥呢?

    一万个艹泥马飞奔而过。。。你猜怎么着,我无论将哪个表单移动到通讯地址这块儿位置它就无法验证了。。。把通讯地址移动到别的位置就正常了!!

    这特么是什么鬼。。。。无奈我就想着是不是的问题,

    我就将改为

    ,一切就好了。。。无奈极了。

    其他表单验证无效总结:

    1、prop中使用的变量与表单v-model绑定的变量名不一样导致无法触发。

    prop="enterFee",v-model="forms.enterMoney",无法触发验证规则。

    1. "入门费(元)" class="is-required" prop="enterFee">
    2. <el-input style="width:100%" v-model="forms.enterMoney" placeholder="请输入入门费">el-input>

    2、rules规则中未定义enterFee相关的验证方法导致无法触发验证规则。

    3、el-from-item没有加prop导致无法触发验证规则。

    4、from绑定的表单变量没有在data中定义导致无法触发验证规则。

  • 相关阅读:
    数字化时代,数据就是资产
    PASCAL VOC数据集格式文件夹下文件配置
    四种常用的自动化测试框架
    将 N 叉树编码为二叉树
    【modbus协议】Modbus-TCP消息帧格式
    介绍Golang的并发模型
    Reggie外卖项目 —— 分类管理模块之新增分类功能
    C++ STL进阶与补充(string容器)
    轮胎球类充气泵方案-充气泵PCBA
    Soft-NMS – Improving Object Detection With One Line of Code
  • 原文地址:https://blog.csdn.net/yunhuaikong/article/details/132920254