Vuetify自定义验证以确认密码

时间:2019-09-27 08:11:21

标签: javascript validation vue.js vuetify.js

我正在使用Vue.js模板,并且在注册页面上,我需要在用户注册时比较密码,因此,我添加了自定义验证规则,例如以下代码:

<v-text-field 
    label="Password" 
    v-model="password" 
    :rules="passwordRules" 
    type="password" 
    required
></v-text-field>
<v-text-field 
    label="Confirm Password" 
    v-model="confirmPassword" 
    :rules="[confirmPasswordRules,passwordConfirmationRule]"
    type="password" 
    required
></v-text-field>

脚本:

data() {
    return {
        password: "",
        confirmPassword: "",
        passwordRules: [v => !!v || "Password is required"],
        confirmPasswordRules: [v => !!v || "Password is required"],
    };
},

比较计算出的密码方法:

computed: {
    passwordConfirmationRule() {
        return () => (this.password === this.confirmPassword) || 'Password must match'
    },
}

我使用计算方法来确认密码是否可以正常工作,并完美地比较了密码,但是它在控制台[Vuetify] Rules should return a string or boolean, received 'object' instead中显示错误,所以我该如何解决这个问题?

4 个答案:

答案 0 :(得分:6)

您可以使用

模板:

<v-text-field
  v-model="password"
  label="Password"
  name="password"
  prepend-icon="mdi-lock"
  type="password"
  :rules="passwordRules"
/>

<v-text-field
  v-model="confirmPassword"
  label="Confirm Password"
  name="confirmPassword"
  prepend-icon="mdi-lock"
  type="password"
  :rules="confirmPasswordRules"
/>

脚本:

data() {
    return {
      password: '',
      confirmPassword: '',
      passwordRules: [
        (value) => !!value || 'Please type password.',
        (value) => (value && value.length >= 6) || 'minimum 6 characters',
      ],
      confirmPasswordRules: [
        (value) => !!value || 'type confirm password',
        (value) =>
          value === this.password || 'The password confirmation does not match.',
      ],
    }
},

答案 1 :(得分:0)

模板

<v-text-field
  label="Password"
  v-model="password"
  :rules="[rules.passwordRules]"
  type="password"
  required>
</v-text-field>
<v-text-field
  label="Confirm Password"
  v-model="confirmPassword"
  :rules="[rules.confirmPasswordRules, passwordConfirmationRule]"
  @update:error="checkPassword"
  type="password"
  required>
</v-text-field>

脚本

data() {
  return {
    password: "",
    confirmPassword: "",
    validPassword: "",
    rules: {
      passwordRules: v => !!v || "Password is required",
      confirmPasswordRules: v => !!v || "Password is required"
    }
  };
},
methods: {
  checkPassword(invalid) { 
    // correct: false
    if (true == invalid) {
      this.validPassword = false;
    } else {
      this.validPassword = true;
    }
   },
 }
  

文本字段提供“ update:error”事件。如果密码有效,它将执行该事件的功能并返回false。   从有效密码更改为无效密码后,对函数返回true。

答案 2 :(得分:0)

您收到错误消息,因为“确认密码”输入的rules属性不包含包含规则的一维数组,而是包含confirmPasswordRules,这是一个数组本身加上passwordConfirmationRule规则。

基本上就是这个

:rules="[confirmPasswordRules, passwordConfirmationRule]"

包含以下内容:

:rules="[[v => !!v || "Password is required"], (this.password === this.confirmPassword) || 'Password must match']"

您希望所有规则都包含在单个数组中。您可以使用concat方法将passwordConfirmationRule添加到confirmPasswordRules数组中,如下所示:

:rules="confirmPasswordRules.concat(passwordConfirmationRule)" 

我创建了一个Codepen来证明它有效here

答案 3 :(得分:0)

我找到了另一种解决问题的方法,我认为对于任何寻求解决方案的人来说都是值得的。

在我的模板中,我有以下内容:

<v-text-field
   v-model.trim="passwordRepeat"
   label="Confirm Password"
   type="password"
   autocomplete="new-password"
   prepend-icon="mdi-lock-check"
   required
   :rules="repeatPasswordRules"
/>

在我的脚本部分:

computed: {
  repeatPasswordRules() {
    return [
      (v) => !!v || 'Senha não informada',
      (v) => (v && v.length >= 8) || 'A senha deve ter no mínimo 8 caracteres',
      (v) => (v === this.password) || 'Senhas diferentes!',
    ];
  },
},

当然,不要忘记验证调用

validate() {
  const valid = this.$refs.signup.validate();
  if(valid) {      
    //your actions after validation
  }
}