我用的是Element+vue,再修改密码的时候,发现不论我怎么修改,怎么输入数据,返现总是给我错误提示(原因是校验错误),发现我怎么输入数据,在离焦之后,校验的结果总是错误的。
<script setup>
import { ref } from 'vue'
const ModifyPassword=ref({
"old_pwd":"",
"new_pwd":"",
"re_pwd":""
})
const userInfo=ref({})
const checkNewAndOld=(rules,value,callback)=>{
//rule表示当前规则,value代表的是值,callback表示如果失败了,那几回调
if(value===''){
callback(new Error('请输入确认密码'));
}else if(value!==ModifyPassword.value.new_pwd){
callback(new Error( '前后输入的密码不一致'))
}else{
callback();//正常回退
}
}
const rules = {
password:[
{ required: true, message: '请输入原密码', trigger: 'blur' }
],
newPassword:[
{ required: true, message: '请输入新密码', trigger: 'blur' },
{ min: 5, max: 16, message: '密码长度为5-16的非空字符串', trigger: 'blur' },
],
rePassword:[
{validator:checkNewAndOld, trigger: 'blur'}
]
}
import useUserInfoStore from '@/store/userInfo.js'
//从pinia中获取用户信息
const userInfoStore = useUserInfoStore();
userInfo.value = userInfoStore.userInfo;
</script>
<template>
<el-card class="page-container">
<template #header>
<div class="header">
<span>基本资料</span>
</div>
</template>
<el-row>
<el-col :span="12">
<el-form :model="ModifyPassword" :rules="rules" label-width="100px" size="large">
<el-form-item label="原始密码" prop="password">
<el-input v-model="ModifyPassword.old_pwd"></el-input>
</el-form-item>
<el-form-item label="新的密码" prop="newPassword">
<el-input v-model="ModifyPassword.new_pwd"></el-input>
</el-form-item>
<el-form-item label="确认密码" prop="rePassword">
<el-input v-model="ModifyPassword.re_pwd"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" >修改</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</el-card>
</template>