23

我正在尝试使用 vee validate 来验证使用此代码的密码。

<div>
    <input type="password"
           placeholder="Password"
           v-model="password"
           v-validate="'required|min:6|max:35|confirmed'"
           name="password" />
</div>
<div>
    <span>{{ errors.first('password') }}</span>
</div>
<div>
    <input type="password"
           placeholder="Confirm password"
           v-model="confirmPassword"
           v-validate="'required|target:password'"
           name="confirm_password" />
</div>
<div>
    <span>{{ errors.first('confirm_password') }}</span>
</div>

但它总是说密码确认不匹配。 我的代码出了什么问题?

4

2 回答 2

52

您的密码输入必须有ref="password"- 这就是 vee-validate 找到目标的方式:

<input v-validate="'required'" ... ref="password">(谢谢,赖利)。

确认:{target} - 输入必须与目标输入具有相同的值,由 {target} 指定为目标字段的名称。

此外,您的 Vee Validate 语法存在错误,请更改target:confirmed:

v-validate="'required|target:password'"

应该

v-validate="'required|confirmed:password'"

看看下面的基本示例,它将检查两件事:

  • 第二个输入字段是否有任何输入值?
  • 如果是,第二个输入值是否与第一个输入值匹配?

var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  }
})
body {
  background: #20262E;
  padding: 15px;
  font-family: Helvetica;
}

#app {
  width: 60%;
  background: #fff;
  border-radius: 10px;
  padding: 15px;
  margin: auto;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vee-validate/2.1.1/vee-validate.js"></script>
<script>
  Vue.use(VeeValidate);
</script>


<div id="app">

  <form id="demo">

    <!-- INPUTS -->
    <div class="input-group">
      <h4 id="header"> Enter Password</h4>

      <div class="input-fields">
        <input v-validate="'required'" name="password" type="password" placeholder="Password" ref="password">

        <input v-validate="'required|confirmed:password'" name="password_confirmation" type="password" placeholder="Password, Again" data-vv-as="password">
      </div>
    </div>

    <!-- ERRORS -->
    <div class="alert alert-danger" v-show="errors.any()">
      <div v-if="errors.has('password')">
        {{ errors.first('password') }}
      </div>
      <div v-if="errors.has('password_confirmation')">
        {{ errors.first('password_confirmation') }}
      </div>
    </div>

  </form>
</div>

进一步阅读:https ://baianat.github.io/vee-validate/guide/rules.html#confirmed

于 2018-07-10T13:02:43.943 回答
7

下面的代码适用于我:https ://logaretm.github.io/vee-validate/advanced/cross-field-validation.html#targeting-other-fields

<template>
  <ValidationObserver>
    <ValidationProvider rules="required|password:@confirm" v-slot="{ errors }">
      <input type="password" v-model="password" />
      <span>{{ errors[0] }}</span>
    </ValidationProvider>

    <ValidationProvider name="confirm" rules="required" v-slot="{ errors }">
      <input type="password" v-model="confirmation" />
      <span>{{ errors[0] }}</span>
    </ValidationProvider>
  </ValidationObserver>
</template>

<script>
import { extend } from 'vee-validate';

extend('password', {
  params: ['target'],
  validate(value, { target }) {
    return value === target;
  },
  message: 'Password confirmation does not match'
});

export default {
  data: () => ({
    password: '',
    confirmation: ''
  })
};
</script>
于 2020-04-11T05:40:11.583 回答