vee-validate 提交时的无模型验证不起作用

发布于 2025-01-11 17:30:37 字数 992 浏览 0 评论 0原文

这似乎很简单,但我无法让它工作...

我想验证文件是否仅在单击验证按钮时设置。但 check 方法中的验证结果总是返回 false。

<template>
  <ValidationObserver>
    <form @submit.prevent>
      <ValidationProvider
        ref="aProvider"
        name="file"
        rules="required"
        v-slot="{ errors }"
      >
        <input type="file" />
        <p>{{ errors[0] }}</p>
      </ValidationProvider>
      <button @click="check">validate!</button>
    </form>
  </ValidationObserver>
</template>

<script>
export default {
  methods: {
    async check() {
      const { valid } = await this.$refs.aProvider.validate();
      if (valid) {
        alert("Form has been submitted!");
      }
    },
  },
};
</script>

codesandbox https://codesandbox.io/s/codesandbox- forked-6o7iyt?file=/src/Demo.vue

this seems to be simple one but I can not get it work...

I want to validate if file is set only when I click the validate button. but the validation result in check method always return false.

<template>
  <ValidationObserver>
    <form @submit.prevent>
      <ValidationProvider
        ref="aProvider"
        name="file"
        rules="required"
        v-slot="{ errors }"
      >
        <input type="file" />
        <p>{{ errors[0] }}</p>
      </ValidationProvider>
      <button @click="check">validate!</button>
    </form>
  </ValidationObserver>
</template>

<script>
export default {
  methods: {
    async check() {
      const { valid } = await this.$refs.aProvider.validate();
      if (valid) {
        alert("Form has been submitted!");
      }
    },
  },
};
</script>

codesandbox https://codesandbox.io/s/codesandbox-forked-6o7iyt?file=/src/Demo.vue

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

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

发布评论

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

评论(2

我不在是我 2025-01-18 17:30:37

validate() 方法适用于观察者,而不适用于文档中指定的提供者:https://vee-validate.logaretm.com/v2/guide/components/validation-observer.html#methods

只需设置一个在观察者上 ref 并运行该方法。

<template>
  <ValidationObserver ref="form">
    <form @submit.prevent>
      <ValidationProvider
        name="file"
        rules="required"
        v-slot="{ errors }"
      >
        <input type="file" />
        <p>{{ errors[0] }}</p>
      </ValidationProvider>
      <button @click="check">validate!</button>
    </form>
  </ValidationObserver>
</template>

<script>
export default {
  methods: {
    async check() {
      const { valid } = await this.$refs.form.validate();
      if (valid) {
        alert("Form has been submitted!");
      }
    },
  },
};
</script>

The validate() method is for Observers, not for providers as specified in docs: https://vee-validate.logaretm.com/v2/guide/components/validation-observer.html#methods

Just set a ref on your Observer and run the method.

<template>
  <ValidationObserver ref="form">
    <form @submit.prevent>
      <ValidationProvider
        name="file"
        rules="required"
        v-slot="{ errors }"
      >
        <input type="file" />
        <p>{{ errors[0] }}</p>
      </ValidationProvider>
      <button @click="check">validate!</button>
    </form>
  </ValidationObserver>
</template>

<script>
export default {
  methods: {
    async check() {
      const { valid } = await this.$refs.form.validate();
      if (valid) {
        alert("Form has been submitted!");
      }
    },
  },
};
</script>
悲念泪 2025-01-18 17:30:37

创建一个数据属性并通过 v-model 将其绑定到输入。将您的引用移至 ValidationObserver。然后它将正确验证。

Create a data property and bind it to the input via v-model. Move your ref to the ValidationObserver. Then it will validate properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文