OnInvalidSubmit剂量在Vee-Validate版本中不起作用(^4.5.2)

发布于 2025-01-31 20:01:02 字数 1412 浏览 0 评论 0 原文

我正在尝试以形式捕获错误,并且必须以模式显示它们。但是Vee估计的OnInvaidSubmit似乎不起作用。请参阅下面的代码。

PS:我从

vue版本:3,vee-validate:4.5.11

<template>
  <Form v-slot="{ validate }" :validation-schema="schema">
    <Field name="email" type="email" />
    <ErrorMessage name="email" />
    <Field name="password" type="password" />
    <ErrorMessage name="password" />
    <button @click="validate">Submit</button>
  </Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate'
import * as yup from 'yup'
export default {
  components: {
    Form,
    Field,
    ErrorMessage,
  },
  data() {
    const schema = yup.object({
      email: yup.string().required().email(),
      password: yup.string().required().min(8),
    })
    return {
      schema,
    }
  },
  methods: {
    onSubmit(values) {
      // Submit values to API...
      alert(JSON.stringify(values, null, 2))
    },
    onInvalidSubmit({ values, errors, results }) {
      console.log(values) // current form values
      console.log(errors) // a map of field names and their first error message
      console.log(results) // a detailed map of field names and their validation results
    },
  },
}
</script>

I am trying to capture errors in forms and have to display them in modal. But vee-validate onInvaidSubmit seems not working. See code below.

PS: I copied this example from https://vee-validate.logaretm.com/v4/guide/components/handling-forms

Vue Version : 3, vee-validate: 4.5.11

<template>
  <Form v-slot="{ validate }" :validation-schema="schema">
    <Field name="email" type="email" />
    <ErrorMessage name="email" />
    <Field name="password" type="password" />
    <ErrorMessage name="password" />
    <button @click="validate">Submit</button>
  </Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate'
import * as yup from 'yup'
export default {
  components: {
    Form,
    Field,
    ErrorMessage,
  },
  data() {
    const schema = yup.object({
      email: yup.string().required().email(),
      password: yup.string().required().min(8),
    })
    return {
      schema,
    }
  },
  methods: {
    onSubmit(values) {
      // Submit values to API...
      alert(JSON.stringify(values, null, 2))
    },
    onInvalidSubmit({ values, errors, results }) {
      console.log(values) // current form values
      console.log(errors) // a map of field names and their first error message
      console.log(results) // a detailed map of field names and their validation results
    },
  },
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2025-02-07 20:01:02

通过将其添加到表单元素中来尝试以下内容。见下文

<Form v-slot="{ validate }" :validation-schema="schema" @invalid-submit="onInvalidSubmit">
  <Field name="email" type="email" />
  <ErrorMessage name="email" />
  <Field name="password" type="password" />
  <ErrorMessage name="password" />
  <button @click="validate">Submit</button>
</Form>

try the following by adding it to the Form element. see below

<Form v-slot="{ validate }" :validation-schema="schema" @invalid-submit="onInvalidSubmit">
  <Field name="email" type="email" />
  <ErrorMessage name="email" />
  <Field name="password" type="password" />
  <ErrorMessage name="password" />
  <button @click="validate">Submit</button>
</Form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文