微信小程序/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 否则没有任何效果

相关推荐
大米饭消灭者3 天前
Taro是怎么实现一码多端的【底层原理】
微信小程序·taro
FliPPeDround4 天前
Vitest Environment UniApp:让 uni-app E2E 测试变得前所未有的简单
微信小程序·e2e·前端工程化
FliPPeDround4 天前
微信小程序自动化的 AI 新时代:wechat-devtools-mcp 智能方案
微信小程序·ai编程·mcp
码云数智-大飞4 天前
如何创建自己的小程序,码云数智与有赞平台对比
微信小程序
luffy54594 天前
微信小程序页面使用类似filter函数的wxs语法
微信小程序·小程序
Slow菜鸟4 天前
微信小程序开发(二)目录结构完全指南
微信小程序·小程序
攀登的牵牛花4 天前
给女朋友写了个轻断食小程序:去老丈人家也是先动筷了
前端·微信小程序
前端小雪的博客.4 天前
【保姆级教程】uniAI 插件高效开发 uni-app 微信小程序(附实战案例)
微信小程序·uni-app·ai编程·uniai
一叶星殇4 天前
微信小程序请求拦截器踩坑:避免重复刷新 token
微信小程序·小程序
笨笨狗吞噬者5 天前
【uniapp】小程序端解决分包的uni_modules打包后产物进入主包中的问题
前端·微信小程序·uni-app