微信小程序/vue3/uview-plus form兜底校验

效果图

代码

javascript 复制代码
<template>
    <u-form :model="form" ref="formRole" :rules="rules">
        <u-form-item prop="nickname">
            <u-input v-model="form.nickname" placeholder="姓名" border="none" />
        </u-form-item>
        <u-form-item prop="password">
            <u-input v-model="form.password" placeholder="密码" border="none" />
        </u-form-item>
        <button @click="submit" class="btn">提交</button>
    </u-form>
</template>

<script setup lang="ts">
    import { reactive, ref } from 'vue';
    const formRole = ref<any>(null);
    const rules = {
        'password': {
            type: 'string',
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change'],
        },
        'nickname': {
            type: 'string',
            required: true,
            message: '请输入名称',
            trigger: ['blur', 'change'],
        }
    }
    const form = reactive({
        nickname: null,
        password: null,
    });
    const submit = () => {
        console.log(formRole.value, 'formRole.value');
        formRole.value.validate().then(res => {
            // uni.$u.toast('校验通过')
            console.log(res, '成功');
            // return
        }).catch(err => {
            console.log(err, '校验失败');
            // return
        })
    };
</script>

经验之谈

1. :model='form'

若把form的值设置为 空对象 const form = reactive({ });

控制台报错

2. 不能把 prop 写为name 否则没有任何效果

相关推荐
toooooop82 天前
微信小程序轮播图高度自适应优化
微信小程序·小程序
StarChainTech2 天前
电动车租赁行业的核心需求:智能中控设备的选择与技术方案
物联网·微信小程序·小程序·软件需求·共享经济
计算机毕设指导62 天前
基于微信小程序的积分制零食自选平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
云起SAAS2 天前
老年美文文章图文短视频资讯阅读抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·老年美文文章图文短视频资讯阅读
qq_12498707532 天前
基于微信小程序的民宿预订系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·毕业设计
一室易安2 天前
uniapp+vue3 微信小程序中 页面切换tab 页面滚动到指定锚点位置,滚动页面时候到达指定锚点位置吸顶tab 会自动进行切换
微信小程序·uni-app·notepad++
不想吃菠萝2 天前
pc端微信小程序post传递data是字符串,自动加了双引号问题修改方案
微信小程序·小程序
宠友信息2 天前
打造可持续增长的垂直社区:仿小红书平台功能体系与架构深度解析
java·微服务·微信小程序·springboot·uniapp
一点晖光3 天前
微信小程序输入框光标从中间删除跳到最后bug解决
微信小程序·uniapp·ios光标
小皮虾3 天前
护航隐私!小程序纯前端“证件加水印”:OffscreenCanvas 全屏平铺实战
前端·javascript·微信小程序