vee-validate 提交时的无模型验证不起作用
这似乎很简单,但我无法让它工作...
我想验证文件是否仅在单击验证按钮时设置。但 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
validate()
方法适用于观察者,而不适用于文档中指定的提供者:https://vee-validate.logaretm.com/v2/guide/components/validation-observer.html#methods只需设置一个在观察者上
ref
并运行该方法。The
validate()
method is for Observers, not for providers as specified in docs: https://vee-validate.logaretm.com/v2/guide/components/validation-observer.html#methodsJust set a
ref
on your Observer and run the method.创建一个数据属性并通过 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.