From a0d1834f85b51173e02357f8a638521612fa06be Mon Sep 17 00:00:00 2001
From: wjt <1797368093@qq.com>
Date: 星期三, 19 六月 2024 16:44:37 +0800
Subject: [PATCH] 添加校验

---
 src/views/infomanger/banner/index.vue |   42 +++++++++++++++++++++++++++++++++---------
 1 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/src/views/infomanger/banner/index.vue b/src/views/infomanger/banner/index.vue
index cd116d6..eb09374 100644
--- a/src/views/infomanger/banner/index.vue
+++ b/src/views/infomanger/banner/index.vue
@@ -1,7 +1,7 @@
 <template>
   <div class="app-container">
     <div class="form-content">
-      <el-form inline :model="form.questionList" label-width="75px" :rules="rules">
+      <el-form inline :model="form.questionList" ref="formRef" label-width="75px" :rules="rules">
         <div
           class="border"
           v-for="(item, index) in form.questionList"
@@ -17,7 +17,12 @@
           </div>
           <el-row>
             <el-col :span="12">
-              <el-form-item :label="`璇勪环椤�${index + 1}`" style="width: 100%" :prop="item.questionName">
+              <el-form-item :label="`璇勪环椤�${index + 1}`" :prop="`${index}`" style="width: 100%"  :rules="[
+                {
+                  validator: validateSubField,
+                  trigger: 'blur'
+                }
+              ]">
                 <el-input
                   placeholder="璇疯緭鍏�"
                   v-model="item.questionName"
@@ -83,7 +88,7 @@
       </el-form>
       <div class="center mt20 fixed">
         <el-button type="primary" @click="addContent">娣诲姞璇勪环椤�</el-button>
-        <el-button @click="save" type="primary" >淇濆瓨鍐呭</el-button>
+        <el-button @click="save()" type="primary" >淇濆瓨鍐呭</el-button>
       </div>
     </div>
   </div>
@@ -94,18 +99,21 @@
 import { ElMessage, ElMessageBox} from 'element-plus'
 const tableData = ref([])
 const addNewRef = ref()
+const formRef = ref()
 const form: any = ref({
   questionList: []
 })
 const rules = ref({
   questionName: [
     {
-      required: true,
-      message: '璇疯緭鍏ラ棶棰�',
-      trigger: 'blur'
+        required: true,
+        message: '璇疯緭鍏ラ棶棰�',
+        trigger: 'blur'
     }
   ]
 })
+const instance = getCurrentInstance()
+
 function getListEvaluate() {
   listEvaluate().then((val) => {
     form.value.questionList = val.data.questionList
@@ -116,10 +124,11 @@
     questionStatus: 0,
     questionType: 2,
     questionScore: 5,
+    questionName: '',
     answerList: []
   })
 }
-function addOptions(index: number, name){
+function addOptions(index: number, name: any){
   if(!form.value.questionList[index].answerList) {
     form.value.questionList[index].answerList = []
   }
@@ -128,9 +137,24 @@
   })
 }
 function save(item?: string) {
-  saveEvaluate({questionList: form.value.questionList}).then(val => {
-    ElMessage.success(item||'淇濆瓨鎴愬姛')
+  formRef.value.validate((valid) => {
+    console.log(valid)
+    if(valid) {
+      saveEvaluate({questionList: form.value.questionList}).then(val => {
+        ElMessage.success(item||'淇濆瓨鎴愬姛')
+      })
+    }
   })
+
+}
+function validateSubField(rule: any, value: any, callback: any) {
+  // console.log(rule, value)
+//   console.log(proxy['form'])
+  if(!form.value.questionList[rule.field].questionName) {
+    callback(new Error('璇峰叆璇勪环椤�'))
+  } else {
+    callback()
+  }
 }
 function changeValue(event, index, childIndex) {
   // form.value.questionList[index].isScore = event.target.

--
Gitblit v1.9.1