简体   繁体   English

提交表单后显示 vuelidate 错误消息

[英]vuelidate error message show up after form submission

I'm using vuelidate for form validation in vuejs3 and composition api.我在 vuejs3 和组合 api 中使用vuelidate进行表单验证。

After submit form, I call validate.提交表格后,我调用验证。 If validation passes, the form fields should be emptied and vuelidate should be reset.如果验证通过,则应清空表单字段并重置 vuelidate。

I currently do that like so:我目前这样做是这样的:

const submitForm = () => {
  if (!v$.value.$validate()) {
    console.log(`Error : ${form} `)
    return
  } 
  // there will be an axios post request here:
  console.log(form)
  v$.value.$reset()
  Object.keys(form).forEach(v => form[v] = "")
  
}

For some reason, v$.value.$reset() is called also when the form is invalid (so validation passes even when forms are invalid) and Object.keys(form).forEach(v => form[v] = "") causes validation errors so that when the form is submitted correctly, all error messages show up.出于某种原因,当表单无效时也会调用v$.value.$reset() (因此即使 forms 无效,验证也会通过)和Object.keys(form).forEach(v => form[v] = "")导致验证错误,以便在正确提交表单时显示所有错误消息。

How to clear the form without reloading the page and reset vuelidate only after submission without errors?如何在不重新加载页面的情况下清除表单并仅在提交无错误后才重置 vuelidate?

We can use this.$v.$reset();我们可以使用this.$v.$reset(); to clear the error messages and it works fine for me清除错误消息,对我来说效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM