在Vue3组合式API中使用vuelidate在提交之前验证表单。

huangapple go评论56阅读模式
英文:

Validate a form before submit in Vue3 composition api with vuelidate

问题

根据您提供的信息,您在使用Vue.js 3的Composition API和Vuelidate进行表单验证时遇到了问题。您尝试在submitHandler()中调用v$.$validate()时遇到了错误。问题可能在于Vuelidate在Vue.js 3中的使用方式略有不同。

在Vue.js 3中,Vuelidate的使用可能需要略微不同。在Vue.js 3的Composition API中,您可以尝试以下方式来进行表单验证:

首先,确保您已经正确导入了Vuelidate库和所需的验证规则,就像您在代码中所示的那样。

然后,您可以使用withValidation函数来创建一个包装了验证方法的对象。在setup函数中使用withValidation,并将其绑定到表单数据上,如下所示:

import { withValidation } from '@vuelidate/core';

const { $v } = withValidation();
const state = reactive({
  productName: '',
  productPrice: '',
  // 其他表单字段
});

// 在模板中使用 $v 代替 v$

接下来,您可以在模板中使用$v对象来访问表单字段的验证方法,如下所示:

<form @submit="submitHandler">
  <input
    v-model="state.productName"
    :class="{ error: $v.state.productName.$invalid && $v.state.productName.$dirty }"
  >
  <input
    v-model="state.productPrice"
    :class="{ error: $v.state.productPrice.$invalid && $v.state.productPrice.$dirty }"
  >
  <!-- 其他表单字段 -->
  <button type="submit">Submit</button>
</form>

最后,您可以在submitHandler中调用$v.$validate()来执行表单验证,就像这样:

const submitHandler = async () => {
  try {
    const isFormCorrect = await $v.$validate();
    if (!isFormCorrect) {
      // 显示适当的错误消息或处理验证不通过的情况
      console.log('Form validation failed');
      return;
    }
    // 提交表单的逻辑
    console.log('Form submitted');
  } catch (error) {
    console.warn({ error });
  }
};

这种方式应该在Vue.js 3的Composition API中有效,以执行表单验证和处理提交操作。希望这能帮助您解决问题。如果您需要更多的帮助或有其他问题,请随时提问。

英文:

According to documentation example there is the following Vue.js options api way to validate the whole form before submitting it

export default {
  methods: {
    async submitForm () {
      const isFormCorrect = await this.v$.$validate()
      // you can show some extra alert to the user or just leave the each field to show it&#39;s `$errors`.
      if (!isFormCorrect) return
      // actually submit form
    }
  }
}

I am using Vue.js 3 with composition api and simply can't make this work in my case.

In my &lt;template&gt; i have a form

&lt;form
  @submit=&quot;submitHandler&quot;
&gt;
  &lt;input&gt;
    :error=&quot;v$.productName.$invalid &amp;&amp; v$.productName.$dirty&quot;
    @input=&quot;v$.productName.$touch()&quot;
  &lt;/input&gt;
  
  &lt;input&gt;
    :error=&quot;v$.productPrice.$invalid &amp;&amp; v$.productPrice.$dirty&quot;
    @update:model-value=&quot;v$.productPrice.$touch()&quot;
  &lt;/input&gt;

  ...
&lt;/form&gt;

Under &lt;script setup&gt; tag i have the following

import { useVuelidate } from &#39;@vuelidate/core&#39;
import { required, integer, minValue } from &#39;@vuelidate/validators&#39;
...
const state = reactive({
  productName: &#39;&#39;,
  productPrice: &#39;&#39;,
  
  ...
})

const rules = {
  productName: { required, $lazy: true },
  productPrice: { required, integer, minValue: minValue(1), $lazy: true },
  
  ...
  $validationGroups: {
    allProductData: [
      &#39;productName&#39;,
      &#39;productPrice&#39; ,
      
      ...
    ]
  }
}

const v$ = useVuelidate(rules, state)

...

const submitHandler = async () =&gt; {
  try {
    const isFormCorrect = await v$.$validate()
    console.log(&#39;Submit Fired&#39;)
  } catch (error) {
    console.warn({error})
  }
}

submitHandler() gives me an error saying error: TypeError: v$.$validate is not a function. I tried with and without making it async and got the same error.
I also tried to place the same code directly in the &lt;form&gt; @click event handler and it works perfectly fine.

&lt;form
  @submit=&quot;v$.validate()&quot;
&gt;
...
&lt;/form&gt;

Am i missing something ? It seems to me like vuelidate2 v$.methodName() only works in the template which is strange because i recall using it exactly as documentation suggests in my Vue.js 2 applications

答案1

得分: 2

useVuelidate返回一个ref,虽然文档不是很清楚,但可以从一个响应式组合中期待到这个结果。

在模板中,ref会自动解包,但在脚本中,应该这样写:

const isFormCorrect = await unref(v$).$validate()
英文:

useVuelidate returns a ref, this is not well-documented but can be expected from a reactive composable.

Refs are automatically unwrapped in a template, in a script it's supposed to be:

const isFormCorrect = await unref(v$).$validate()

huangapple
  • 本文由 发表于 2023年2月16日 18:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定