【问题标题】:Vuetify custom validation for confirm passwordVuetify 自定义验证以确认密码
【发布时间】:2020-01-27 12:54:07
【问题描述】:

我正在使用 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 中显示错误,我该如何解决这个问题??

【问题讨论】:

    标签: javascript validation vue.js vuetify.js


    【解决方案1】:

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

    所以本质上是这样的

    :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

    【讨论】:

      【解决方案2】:

      你可以使用

      模板:

      <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.',
            ],
          }
      },
      

      【讨论】:

      • this.passwordconfirmPasswordRules 中始终为空。你们知道为什么吗?
      • 有趣,如果我把这条规则放在标签本身,它就起作用了。例如::rules="[!!confirmPassword || 'type confirm password', password === confirmPassword || 'The password confirmation does not match.']"
      【解决方案3】:

      模板

      <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;
          }
         },
       }
      

      文本字段提供“更新:错误”事件。如果密码有效,则执行该事件的函数并返回 false。 当函数从有效密码更改为无效密码时,返回 true。

      【讨论】:

      • 那么我怎样才能达到我想要的呢?如果规则应返回字符串或布尔值,则改为接收“函数”。根据您的回答,我只是使用必填字段进行验证,但我需要确认密码和必填字段。
      • 对不起,我犯了一个错误。我把计算属性误认为是方法。我更新了我的文章。
      • 我不想检查有效或无效密码我只想比较两个密码以进行注册,但您的代码没有在任何地方比较密码。
      【解决方案4】:

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

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

      <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
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2015-04-03
        • 2023-02-23
        • 1970-01-01
        • 2011-08-23
        • 2017-12-06
        相关资源
        最近更新 更多