当前位置: 首页 > news >正文

uniapp动态循环表单校验失败:初始值校验

问题现象 💥

在实现动态增减的单价输入表单时(基于uv-form组件),遇到以下诡异现象:

  • <uv-input>的v-model绑定初始值为数字类型时,required规则失效 ❌
  • 数字类型与字符串类型校验表现不一致 🔢

技术栈背景 🛠️

  • 框架:Vue3 + uni-app
  • UI库:uv-ui
  • 校验方案:计算属性动态生成规则

示意图 🔍

在这里插入图片描述

解决方案 🚀

1.优先使用字符串类型做表单绑定
2.添加自定义校验规则(validator)解决初始值问题

<template>
  <view class="form-box">
    <uv-form ref="refForm" :model="formvalue" labelWidth="100px" :rules="rules">
      <view class="form-list-item" v-for="(item, index) in formvalue.detailList" :key="index">
        <uv-form-item borderBottom label="单价" :prop="`detailList.${index}.amount`" required>
          <uv-input inputAlign="right" type="digit" v-model="item.amount" placeholder="请输入单价" border="none">
            <template v-slot:suffix></template>
          </uv-input>
        </uv-form-item>
        <view class="mt24" v-if="formvalue.detailList.length != 1"><uv-button plain text="移除"
            @click.stop="handleDel(index)" :customStyle="{ height: '60rpx' }"></uv-button>
        </view>
      </view>
    </uv-form>
    <view class="page-footer">
      <uv-button plain text="新增" @click.stop="handleAdd()"></uv-button>
      <uv-button type="primary" shape="circle" text="提交" @click="onSubmit"></uv-button>
    </view>
  </view>
</template>
<script setup>
import { ref, reactive, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { deepClone, isEmpty } from '@/utils';
const refForm = ref(null);
const formvalue = reactive({
  detailList: [],
});
const billRules = computed(() => {
  let rules = {};
  formvalue.detailList.forEach((item, index) => {
    rules[`detailList.${index}.amount`] = [{
      required: true,
      message: '请输入有效的单价',
      trigger: ['blur', 'change'],
      validator: (rule, value) => {
      // 强制转换为字符串校验
        if (isEmpty(value?.toString())) return false; 
        const numValue = Number(value);
        return !isNaN(numValue) && numValue > 0;
      }
    }];
  })
  return rules
});
const rules = computed(() => {
  return {
    ...billRules.value
  }
});
const detailObj = {
  amount: 10,
}
const handleAdd = () => {
  formvalue.detailList.push(deepClone(detailObj))
}
const handleDel = (index) => {
  formvalue.detailList.splice(index, 1)
}
onLoad(async () => {
  handleAdd()
})
const onSubmit = async () => {
  refForm.value.validate().then(async () => {

  })
}   
</script>
<style lang="scss" scoped>
.form-box {
  margin-top: 180rpx;
}

.page-footer {
  position: fixed;
  width: 100%;
  height: 120rpx;
  bottom: 0;
  background-color: #fff;
  padding: 65rpx 32rpx 80rpx 32rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .uv-button-box {
    flex: 1;
    margin: 0 12rpx;
  }
}
</style>

相关文章:

  • 【学习资源】多元时序分析问题和时序图模型
  • 【机器学习】建模流程
  • uniapp笔记-swiper组件实现轮播图
  • Unicode代码点与编码
  • 好消息!软航文档控件(NTKO WebOffice)在Chrome 133版本上提示扩展已停用的解决方案
  • 【虚拟环境配置】地信从0开始的环境配置小白教程
  • python康复日记-request库的使用,爬虫自动化测试
  • 【2025】基于springboot+vue的校园创新创业竞赛平台设计与实现(源码、万字文档、图文修改、调试答疑)
  • 使用Mastra.ai构建AI智能体:一次动手实践
  • 年龄预测识别模型训练python代码
  • PostgreSQL 事务处理
  • AI大模型全攻略:原理 · 部署 · Prompt · 场景应用
  • AI本地部署之dify
  • 【LNMP】网站架构分布式部署
  • 智能体(Agent)入门,智能体构建方法、工具链与应用策略;大语言模型的本地化部署、优化
  • 【AI学习从零至壹】PyTorch模型训练相关要素
  • 亚马逊云科技全面托管DeepSeek-R1模型现已上线
  • 第二节第一部分:String字符串
  • 建库字符集选择`utf8mb4` + `utf8mb4_unicode_ci` 组合
  • 第三讲 | C/C++内存管理完全手册
  • 弘扬 “上海精神”,上合组织政党论坛聚焦政党责任与使命
  • 技术派|“会飞的手榴弹”:微型无人机将深刻改变单兵作战方式
  • 秦洪看盘|避险情绪升温,短线或延续相对钝化状态
  • 冲击一英里4分钟大关,基普耶贡挑战女子中长跑极限
  • 特朗普支持率降至新低:宣布关税后骤降,选民最不满经济表现
  • 海关总署:明确部分货物、物品不再按进出境特殊物品监管