fix validator

This commit is contained in:
jxxghp
2023-06-28 08:38:26 +08:00
parent 30737d297d
commit 7401ef7941
3 changed files with 19 additions and 7 deletions
+4
View File
@@ -0,0 +1,4 @@
import { ValidationRule } from 'vuetify/types/services/validation'
// 必输校验
export const requiredValidator: ValidationRule = (value: any) => !!value || '此项为必填项'
+8 -6
View File
@@ -1,7 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { requiredValidator } from '@/@validators';
import api from '@/api'; import api from '@/api';
import router from '@/router'; import router from '@/router';
import logo from '@images/logo.svg?raw'; import logo from '@images/logo.svg?raw';
import type { VForm } from 'vuetify/components/VForm';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
// Vuex Store // Vuex Store
@@ -13,6 +15,7 @@ const form = ref({
password: '', password: '',
remember: true, remember: true,
}) })
const refForm = ref<InstanceType<typeof VForm> | null>(null)
// 密码输入 // 密码输入
const isPasswordVisible = ref(false) const isPasswordVisible = ref(false)
@@ -38,9 +41,8 @@ const fetchBackgroundImage = async () => {
// 登录获取token事件 // 登录获取token事件
const login = () => { const login = () => {
errorMessage.value = '' errorMessage.value = ''
if (!form.value.username || !form.value.password) { // 进行表单校验
errorMessage.value = '请输入用户名和密码' if (refForm.value && !refForm.value.validate()) {
return return
} }
@@ -133,7 +135,7 @@ onMounted(() => {
</VCardText> </VCardText>
<VCardText> <VCardText>
<VForm @submit.prevent="login"> <VForm @submit.prevent="login" ref="refForm">
<VRow> <VRow>
<!-- username --> <!-- username -->
<VCol cols="12"> <VCol cols="12">
@@ -141,7 +143,7 @@ onMounted(() => {
v-model="form.username" v-model="form.username"
label="用户名" label="用户名"
type="text" type="text"
required :rules="[requiredValidator]"
/> />
</VCol> </VCol>
@@ -154,7 +156,7 @@ onMounted(() => {
:append-inner-icon=" :append-inner-icon="
isPasswordVisible ? 'mdi-eye-off-outline' : 'mdi-eye-outline' isPasswordVisible ? 'mdi-eye-off-outline' : 'mdi-eye-outline'
" "
required :rules="[requiredValidator]"
@click:append-inner="isPasswordVisible = !isPasswordVisible" @click:append-inner="isPasswordVisible = !isPasswordVisible"
/> />
+7 -1
View File
@@ -29,12 +29,18 @@
"@core": [ "@core": [
"src/@core" "src/@core"
], ],
"@validators": [
"src/@validators"
],
"@validators/*": [
"src/@validators/*"
],
"@images/*": [ "@images/*": [
"src/assets/images/*" "src/assets/images/*"
], ],
"@styles/*": [ "@styles/*": [
"src/styles/*" "src/styles/*"
] ],
}, },
"lib": [ "lib": [
"esnext", "esnext",