From e996a1bae60e1620c8c8074d055e05965eb67b26 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Thu, 12 Oct 2023 09:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=AA=8C=E8=AF=81=E7=9A=84?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 1 + src/router/index.js | 6 ++++ src/views/Form_Demo3.vue | 59 ++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/App.vue b/src/App.vue index bd305ce..317647b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,6 +2,7 @@
Form Demo 1: 基本使用
Form Demo 2: 行内表单
+ Form Demo 3: 表单验证

diff --git a/src/router/index.js b/src/router/index.js index 0abf342..a5bec65 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -4,6 +4,7 @@ import {createRouter, createWebHistory} from 'vue-router' // const root = () => import('../App.vue') const formDemo1 = () => import('../views/Form_Demo1.vue') const formDemo2 = () => import('../views/Form_Demo2.vue') +const formDemo3 = () => import('../views/Form_Demo3.vue') // 引入组件 @@ -21,6 +22,11 @@ const routes = [ name: 'form2', component: formDemo2 }, + { + path: '/form3', + name: 'form3', + component: formDemo3 + }, ] // 创建路由器,使用createRouter方法,传入history(路由类型)和 创建好的路由routes diff --git a/src/views/Form_Demo3.vue b/src/views/Form_Demo3.vue index 23a4233..14725c1 100644 --- a/src/views/Form_Demo3.vue +++ b/src/views/Form_Demo3.vue @@ -1,9 +1,9 @@ @@ -93,29 +97,26 @@ export default { } ], type: [{required: true, type: 'array', message: '至少选择一个活动类型', trigger: 'change'}], - resource: [{required: true, message: '请选择活动资源', trigger: 'change'}] + resource: [{required: true, message: '请选择活动资源', trigger: 'change'}], + number: [{required: true, message: '请输入参与人数', trigger: 'blur'}], + desc: [{required: true, message: '请填写备注', trigger: 'blur'}] }, } }, methods: { - submit() { - console.log('created') - console.log(this.form) - }, - cancel() { - console.log('cleared') - this.form = { - name: "", - region: "", - date1: "", - date2: "", - delivery: false, - type: [], - resource: "", - number: "", - desc: "", - } + formSubmit(formName){ + this.$refs[formName].validate((valid) => { + if (valid) { + alert('提交成功!'); + }else{ + console.log('提交失败'); + return false; + } + }) }, + formReset(formName){ + this.$refs[formName].resetFields() + } } }