uniapp的form表单自定义验证规则

2023-12-16 09:41:23
?template部分:
<uni-forms ref="form" :value="user" labelWidth="80px">
			<uni-forms-item name="oldPassword" label="旧密码">
				<uni-easyinput type="password" v-model="user.oldPassword" placeholder="请输入旧密码" />
			</uni-forms-item>
			<uni-forms-item name="newPassword" label="新密码">
				<uni-easyinput type="password" v-model="user.newPassword" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item name="confirmPassword" label="确认密码">
				<uni-easyinput type="password" v-model="user.confirmPassword" placeholder="请确认新密码" />
			</uni-forms-item>
			<button type="primary" @click="submit">提交</button>
		</uni-forms>
?data部分:
user: {
					oldPassword: undefined,
					newPassword: undefined,
					confirmPassword: undefined
				},
				rules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: '旧密码不能为空'
						}]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: '新密码不能为空',
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: '长度在 6 到 20 个字符'
							}
						]
					},
					confirmPassword: {
						rules: [{
							required: true,
							errorMessage: '确认密码不能为空'
						}, {
							validateFunction: (rule, value, data) => data.newPassword === value,
							errorMessage: '两次输入的密码不一致'
						}]
					}
				}
methods部分:
submit() {
				this.$refs.form.validate().then(res => {
				updateUserPwd(this.user.oldPassword,this.user.newPassword).then(response =>           {
						this.$modal.msgSuccess("修改成功")
					})
				})
			}

文章来源:https://blog.csdn.net/m0_55333789/article/details/135028559
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。