简体   繁体   English

VeeValidation 始终返回 true

[英]VeeValidation returns always true

I just start using vee-validate in my project.我刚开始在我的项目中使用 vee-validate。 I have following code in my component where is simple form group with validation我的组件中有以下代码,其中带有验证的简单表单组

    <ValidationObserver ref="observer" v-slot="{ invalid }">
        <b-form @submit.prevent="onSubmit" novalidate>
            <b-form-group label="Amount">
                <ValidationProvider name="amount" rules="required|min_value:0" v-slot="{ errors }">
                    <b-form-input
                        :state="errors.length == 0"
                        v-model="form.amount"
                        type="text"
                        placeholder="Amount"
                    ></b-form-input>
                    <b-form-invalid-feedback :state="errors.length == 0">{{errors.join('. ')}}</b-form-invalid-feedback>
                </ValidationProvider>
            </b-form-group>
        </b-form>
    </ValidationObserver>

I have imported ValidationObserver and ValidationProvider in component but validation behave weird.我在组件中导入了 ValidationObserver 和 ValidationProvider 但验证行为很奇怪。

在此处输入图片说明

On picture you can see default behavior.在图片上,您可以看到默认行为。 It is green from start and doesn't matter what I write or if I submit as empty.它从一开始就是绿色的,无论我写什么或者我是否提交为空都无关紧要。 It is always green.它永远是绿色的。

Submit method looks like this:提交方法如下所示:

       async onSubmit() {
            let validate = await this.$refs.observer.validate();
            console.log('VALID: ', validate)
        },

and gives me always true.并给我永远真实。

I am using nuxt 2.9.x and vee-validate 3.1.x我正在使用 nuxt 2.9.x 和 vee-validate 3.1.x

Using vee-validate, you have to explicitly extend vee-validate with any rules you want.使用 vee-validate,您必须使用您想要的任何规则显式extend vee-validate。 There is a specific example here in the documentation that covers how to do that:有一个具体的例子在这里的文件,讨论了如何做到这一点的:

import { extend } from 'vee-validate';
import { required, min_value } from 'vee-validate/dist/rules';

extend('min_value', min_value);
extend('required', required);

If you don't do that, your rules are just ignored and the form is always validated as being valid.如果你不这样做,你的规则就会被忽略,并且表单总是被验证为有效的。

Also if you are using nuxt, see the notes here另外,如果您使用的是 nuxt,请参阅此处的注释

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

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