Vuelidate:在单击时验证,而不是在触摸字段时验证

新手上路,请多包涵

我对 vuelidate 有点陌生,一切正常,除了我不知道如何仅在单击 提交 按钮时运行验证。现在,当您开始提供任何输入时,它会将触摸字段标记为红色,我希望它可以等待,直到用户想要提交填写好的表格。

这是我现在得到的:

 Vue.use(window.vuelidate.default)
const { required, minLength, sameAs } = window.validators

new Vue({
	el: "#app",
  data: {
  	user: {
    	login: '',
      password: '',
      repeatedPassword: ''
    }
  },
  validations: {
  	user: {
    	login: {
      	required,
        minLength: minLength(5)
      },
      password: {
    	  required,
        minLength: minLength(8)
      },
      repeatedPassword: {
      	required,
        sameAs: sameAs('password')
      }
    }
  }
})
 input {
  border: 1px solid silver;
  border-radius: 4px;
  background: white;
  padding: 5px 10px;
}

.error {
  border-color: red;
  background: #FDD;
}

.error:focus {
  outline-color: #F99;
}

.valid {
  border-color: #5A5;
  background: #EFE;
}

.valid:focus {
  outline-color: #8E8;
}
 <script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuelidate/dist/validators.min.js"></script>
<script src="https://unpkg.com/vuelidate/dist/vuelidate.min.js"></script>
`<div id="app">

  <input type="text" placeholder="login"
    v-model="user.login"
    v-on:input="$v.user.login.$touch"
    v-bind:class="{error: $v.user.login.$error, valid: $v.user.login.$dirty && !$v.user.login.$invalid}">
  <br/>
  <input type="password" placeholder="password"
    v-model="user.password"
    v-on:input="$v.user.password.$touch"
    v-bind:class="{error: $v.user.password.$error, valid: $v.user.password.$dirty && !$v.user.password.$invalid}">
  <br/>
  <input type="password" placeholder="repeat password"
    v-model="user.repeatedPassword"
    v-on:input="$v.user.repeatedPassword.$touch"
    v-bind:class="{error: $v.user.repeatedPassword.$error, valid: $v.user.repeatedPassword.$dirty && !$v.user.repeatedPassword.$invalid}"
  >
  <button :disabled="$v.user.$error" @click="$v.user.$touch()">
    Submit!
  </button>
</div>`

原文由 nemezis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 293
2 个回答

我永远无法真正习惯 Vuelidate 的做事方式,但一般来说,它是这样工作的: https ://monterail.github.io/vuelidate/#sub-basic-form

像这样设置允许您对每个表单输入/元素进行验证,然后全面检查表单是否“脏”和/或“无效”

 form: {
"name": {
"required": false,
"$invalid": true,
"$dirty": false,
"$error": false,
"$pending": false,
"$params": {
  "required": {
    "type": "required"
  }
}
},
"Age": {
  "required": false,
  "$invalid": true,
  "$dirty": false,
  "$error": false,
  "$pending": false,
  "$params": {
    "required": {
      "type": "required"
    }
  }
},
"$invalid": true,  <------- This is what you are after for valid/invalid
"$dirty": false,   <------- This is what you are after to see if the form has been used.
"$error": false,  <-------  This checks both invalid and dirty
"$pending": false,
"$params": {
   "nestedA": null,
   "nestedB": null
}
}

就在实践中使用它而言,一种选择是在提交时调用 validateform 事件。

 @click.prevent="validateform"

然后在你的 vue 实例中创建一个 validateform 方法来检查

$v.form.$invalid  or $v.form.$error

然后显示错误或调用实际的提交方法

原文由 retrograde 发布,翻译遵循 CC BY-SA 3.0 许可协议

然后,在设置验证后唯一要做的就是调用一个方法来验证错误。所以请遵循以下内容:

 <button @click="validate">Submit</button>

方法:

 validate () {
  this.$v.$touch() //it will validate all fields
  if (!this.$v.$invalid) { //invalid, becomes true when a validations return false
   //you dont have validation error.So do what u want to do here
  }
}

原文由 Roland 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题