• uview 1 uni-app表单 number digit 的输入框有初始化赋值后,但是校验失败


     背景:

    在onReady初始化规则

    onReady() {
                this.$refs.uForm.setRules(this.rules);
     },

    同时:ref,model,rules,props都要配置好。

    报错

    当input框限定type为number,digit类型有初始值不做修改动作,直接提交会报错,验证失败:报空。

    验证规则

    data():{

    return {

    model: {
      type: '年假', days: '0', reason: '-', hours: 0,
    },
    rules: {
      type: [
        {
          required: true,
          message: '请输入请假类型',
          trigger: ['change', 'blur'],
        }
      ],
      days: [
        {
          required: true,
          message: '请输入请假类型',
          trigger: ['change', 'blur'],
        }
      ],
      hours: [
        {
          required: true,
          message: '请输入小时',
          trigger: ['change', 'blur'],
        }
      ],
      reason: [
        {
          required: true,
          message: '请输入租赁洗涤价',
          trigger: ['change', 'blur']
        }]
      // {min: 0,max:500, message: '不能小于0', trigger: ['change', 'blur'],}],
    },
    

    }}

    解决:

    方法1:hours:数字的字段加上数字正则匹配规则,限制只能0-999的数字,且只能有2个小数。

    hours: [
      {
        required: true,
        message: '请输入小时',
        pattern:/^(([1-9]\d{0,3})|0)(\.\d{0,2})?$/,
        trigger: ['change', 'blur'],
      }
    ]

    方法2: hours: 先强制转成字符串再让其检验,如果不限制小数位数,这个方法简单点,

    hours:[{required: true,
    message: '请输入售价',
    trigger: ['change', 'blur'],
    // 正则检验前先将值转为字符串
    transform(value) {
      return String(value);
    },}]

    源码:

    1. <template>
    2. <view class="wrap" style="padding-bottom: 60px;">
    3. <u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
    4. <!-- 当前套餐 -->
    5. <view v-if="current === 0">
    6. <u-form class="form" :model="model" :rules="rules" ref="uForm">
    7. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    8. <u-form-item label="请假类型" label-width="150" right-icon="arrow-right" prop="type">
    9. <u-input placeholder="请选择" type="select" class="form-field-select" v-model="model.type"/>
    10. </u-form-item>
    11. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    12. <u-form-item label="天数" label-width="150" right-icon="arrow-right" prop="days">
    13. <u-input placeholder="请选择" type="text" class="form-field-select" v-model="model.days"/>
    14. </u-form-item>
    15. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    16. <u-form-item label="小时" label-width="150" right-icon="arrow-right" prop="hours">
    17. <u-input placeholder="请选择" type="number" class="form-field-select" v-model="model.hours"/>
    18. </u-form-item>
    19. <u-form-item label="结束时间" label-width="150" right-icon="arrow-right">
    20. <u-input placeholder="请选择" type="" class="form-field-select"/>
    21. </u-form-item>
    22. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    23. <u-form-item label="请假事由" label-width="150" prop="reason"></u-form-item>
    24. <u-form-item>
    25. <u-input type="textarea" placeholder="请输入内容" v-model="model.reason"/>
    26. </u-form-item>
    27. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    28. <u-form-item label="图片" label-width="150">
    29. <view solt="right" style="flex:1;text-align: right;align-items: center;">
    30. <i class="add-user iconfont icon-tupian"></i>
    31. </view>
    32. </u-form-item>
    33. <u-gap height="20" bg-color="#f5f5f5"></u-gap>
    34. <u-form-item label="选人处理人" label-width="150">
    35. <view solt="right" style="flex:1;text-align: right;align-items: center;">
    36. <i class="add-user iconfont icon-zengjia"></i>
    37. </view>
    38. </u-form-item>
    39. </u-form>
    40. <u-row gutter="32" class="bottom-box" justify="center">
    41. <u-col span="10">
    42. <view>
    43. <u-button type="primary" shape="circle" @click="submitForm">确定</u-button>
    44. </view>
    45. </u-col>
    46. </u-row>
    47. </view>
    48. </view>
    49. </template>
    50. <script>
    51. export default {
    52. data() {
    53. return {
    54. show: false,
    55. model: {
    56. type: '年假', days: '0', reason: '-', hours: 1,
    57. },
    58. rules: {
    59. type: [
    60. {
    61. required: true,
    62. message: '请输入请假类型',
    63. trigger: ['change', 'blur'],
    64. }
    65. ],
    66. days: [
    67. {
    68. required: true,
    69. message: '请输入请假类型',
    70. trigger: ['change', 'blur'],
    71. }
    72. ],
    73. hours: [
    74. {
    75. required: true,
    76. message: '请输入请假类型',
    77. pattern:/^(([1-9]\d{0,3})|0)(\.\d{0,2})?$/,
    78. trigger: ['change', 'blur'],
    79. }
    80. ],
    81. reason: [
    82. {
    83. required: true,
    84. message: '请输入原由',
    85. trigger: ['change', 'blur']
    86. }]
    87. // {min: 0,max:500, message: '不能小于0', trigger: ['change', 'blur'],}],
    88. },
    89. list: [{
    90. name: '发起申请'
    91. }, {
    92. name: '查看数据',
    93. }],
    94. m2mSimflowList: [],
    95. m2mOrderFlowList: [],
    96. current: 0,
    97. status: 'loadmore',
    98. iconType: 'circle',
    99. isDot: false,
    100. loadText: {
    101. loadmore: '点击加载更多',
    102. loading: '正在加载...',
    103. nomore: '没有更多了'
    104. },
    105. }
    106. },
    107. onReady() {
    108. this.$refs.uForm.setRules(this.rules)
    109. },
    110. created() {
    111. },
    112. methods: {
    113. submitForm() {
    114. this.$refs.uForm.validate(valid => {
    115. if (valid) {
    116. this.$u.toast("验证通过")
    117. } else {
    118. this.$u.toast('验证失败')
    119. }
    120. })
    121. },
    122. change(index) {
    123. this.current = index;
    124. },
    125. navTo(url) {
    126. uni.navigateTo({
    127. url: url
    128. });
    129. }
    130. }
    131. }

  • 相关阅读:
    Android源码相关面试专题
    全景分割的自监督学习
    计算点云每个点与Z轴的垂直度(附open3d python代码)
    【Prism系列】Region的用法
    (c语言)五子棋<可修改棋数>
    .NET 零开销抽象指南
    pycharm如何优雅的添加gitignore以及查看不同文件状态对应的颜色
    JAVA mybatis操作mysql——批量增加,批量删除,多条件模糊搜索,新增时返回id和常用的增删查改
    【论文笔记】Diffusion-based 3D Object Detection with Random Boxes
    java-net-php-python-ssm电影推荐网站计算机毕业设计程序
  • 原文地址:https://blog.csdn.net/LlanyW/article/details/134027818