vue如何区分新增功能和编辑功能密码框的校验?
时间: 2024-03-04 10:49:39 浏览: 123
在Vue中,可以通过判断当前视图是新增视图还是编辑视图来区分密码框的校验。比如,可以在新增视图中给密码框添加校验规则,而在编辑视图中不添加校验规则。具体实现可以通过以下步骤:
1. 在新增视图中给密码框添加校验规则,比如:
```vue
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="password" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showPassword = !showPassword" icon="el-icon-view"></el-button>
</div>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showConfirmPassword = !showConfirmPassword" icon="el-icon-view"></el-button>
</div>
<el-input v-model="email" autocomplete="off"></el-input>
<el-input v-model="phone" autocomplete="off"></el-input>
<el-input v-model="realname" autocomplete="off"></el-input>
<el-select v-model="sex" placeholder="请选择性别">
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
<el-button type="primary" @click="handleSubmit" :loading="loading">提交</el-button>
</el-form-item>
...
data() {
return {
password: '',
confirmPassword: '',
email: '',
phone: '',
realname: '',
sex: '',
showPassword: false,
showConfirmPassword: false,
loading: false,
rules: {
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ pattern: /^.{6,20}$/, message: '密码长度在6到20个字符', trigger: 'blur' }
],
confirmPassword: [
{ required: true, message: '请再次输入密码', trigger: 'blur' },
{ validator: this.checkPassword, trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
],
phone: [
{ required: true, message: '请输入手机号码', trigger: 'blur' },
{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
],
realname: [
{ required: true, message: '请输入真实姓名', trigger: 'blur' }
],
sex: [
{ required: true, message: '请选择性别', trigger: 'change' }
]
}
}
},
```
2. 在编辑视图中不添加密码框的校验规则,比如:
```vue
<el-form-item label="密码" prop="password" v-if="isAdd">
<el-input type="password" v-model="password" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showPassword = !showPassword" icon="el-icon-view"></el-button>
</div>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showConfirmPassword = !showConfirmPassword" icon="el-icon-view"></el-button>
</div>
<el-input v-model="email" autocomplete="off"></el-input>
<el-input v-model="phone" autocomplete="off"></el-input>
<el-input v-model="realname" autocomplete="off"></el-input>
<el-select v-model="sex" placeholder="请选择性别">
<el-option label="男" value="1"></el-option>
<el-option label="女" value="2"></el-option>
</el-select>
</el-form-item>
...
data() {
return {
password: '',
confirmPassword: '',
email: '',
phone: '',
realname: '',
sex: '',
showPassword: false,
showConfirmPassword: false,
loading: false,
isAdd: true, // 标识当前视图是新增视图还是编辑视图
rules: {
confirmPassword: [
{ required: true, message: '请再次输入密码', trigger: 'blur' },
{ validator: this.checkPassword, trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
],
phone: [
{ required: true, message: '请输入手机号码', trigger: 'blur' },
{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
],
realname: [
{ required: true, message: '请输入真实姓名', trigger: 'blur' }
],
sex: [
{ required: true, message: '请选择性别', trigger: 'change' }
]
}
}
},
```
3. 在编辑视图中通过设置`isAdd`为`false`来标识当前视图为编辑视图,比如:
```vue
<template>
<div>
<el-form :model="formData" :rules="rules" ref="form" label-position="left" label-width="100px" size="medium">
<el-form-item label="用户名" prop="username">
<el-input v-model="formData.username" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="formData.email" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="手机号" prop="phone">
<el-input v-model="formData.phone" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="真实姓名" prop="realname">
<el-input v-model="formData.realname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="性别" prop="sex">
<el-radio-group v-model="formData.sex">
<el-radio label="1">男</el-radio>
<el-radio label="2">女</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="密码" prop="password" v-if="isAdd">
<el-input type="password" v-model="password" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showPassword = !showPassword" icon="el-icon-view"></el-button>
</div>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showConfirmPassword = !showConfirmPassword" icon="el-icon-view"></el-button>
</div>
</el-form-item>
<el-form-item label="确认密码" prop="confirmPassword" v-if="isAdd">
<el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
<div slot="append">
<el-button @click="showConfirmPassword = !showConfirmPassword" icon="el-icon-view"></el-button>
</div>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleSubmit" :loading="loading">提交</el-button>
<el-button @click="handleReset">重置</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
formData: {
id: '',
username: '',
password: '',
email: '',
phone: '',
realname: '',
sex: ''
},
password: '',
confirmPassword: '',
showPassword: false,
showConfirmPassword: false,
loading: false,
isAdd: true, // 标识当前视图是新增视图还是编辑视图
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
{ pattern: /^.{6,20}$/, message: '密码长度在6到20个字符', trigger: 'blur' }
],
confirmPassword: [
{ required: true, message: '请再次输入密码', trigger: 'blur' },
{ validator: this.checkPassword, trigger: 'blur' }
],
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
],
phone: [
{ required: true, message: '请输入手机号码', trigger: 'blur' },
{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
],
realname: [
{ required: true, message: '请输入真实姓名', trigger: 'blur' }
],
sex: [
{ required: true, message: '请选择性别', trigger: 'change' }
]
}
}
},
methods: {
handleSubmit() {
this.$refs.form.validate((valid) => {
if (valid) {
this.loading = true
if (this.isAdd) {
// 新增用户
this.addUser()
} else {
// 编辑用户
this.editUser()
}
} else {
return false
}
})
},
handleReset() {
this.$refs.form.resetFields()
},
checkPassword(rule, value, callback) {
if (value !== this.password) {
callback(new Error('两次输入的密码不一致'))
} else {
callback()
}
},
addUser() {
// 新增用户逻辑
},
editUser() {
// 编辑用户逻辑
}
}
}
</script>
```
在以上代码中,`isAdd`属性用来标识当前视图是新增视图还是编辑视图,如果是新增视图,则在密码框上添加校验规则,如果是编辑视图,则不添加校验规则。同时,在提交表单时,也可以判断是新增用户还是编辑用户,执行不同的逻辑。
阅读全文