【问题标题】:Vee-validate returns always trueVee-validate 总是返回 true
【发布时间】:2018-10-18 06:42:31
【问题描述】:

我有 2 个字段要验证,但在我提交后总是在方法 validateBeforeSubmit() 中返回 true。

Whitout 按钮提交效果很好,并以良好的语言返回 errors.first(name)

我做错了什么?

App.vue:

<form @submit.prevent="validateBeforeSubmit">

    <form-input v-on:input="handleTitle" :validate="'required|email'" label="email" labelvalue="email" type="text" placeholder="" name="email" :value="title" classname="form-control" id=""></form-input>
    <form-input v-on:input="handleLink" :validate="'required'" label="Link" labelvalue="Link" type="text" placeholder="" name="link" :value="link" classname="form-control" id=""></form-input>

methods: {
        validateBeforeSubmit() {
            this.$validator.validateAll().then((result) => {
                if (result) {
                    // eslint-disable-next-line
                    alert('Form Submitted!');
                    return;
                }

                alert('Correct them errors!');
            });
        },

Input.vue:

<template>
     <div>
        <div class="form-group">
            <span>{{ errors.first(name) }}</span>
            <label v-if="label" :for="label" v-html="labelvalue"></label>
            <input v-validate="validate" v-on:input="updateValue($event)" :type="type" :placeholder="placeholder" :name="name" :value="value" :class="classname" :id="id">
        </div>
     </div>
</template>

export default {
    props: {
        validate: String,
        type: String,
        placeholder: String,
        name: String,
        value: String,
        classname: String,
        id: String,
        label: String,
        labelvalue: String
    },
    methods: {
        updateValue: function (evt) {
            this.$emit('input', evt)
        }
    }
}

【问题讨论】:

    标签: vuejs2


    【解决方案1】:

    validateAll 不查看子组件。您必须注入父验证器。在Input.vue 文件中添加inject: ['$validator']。它应该可以解决问题。导出块将如下所示

    export default {
        inject: ['$validator'],
        props: {
            validate: String,
            type: String,
            placeholder: String,
            name: String,
            value: String,
            classname: String,
            id: String,
            label: String,
            labelvalue: String
        },
        methods: {
            updateValue: function (evt) {
                this.$emit('input', evt)
            }
        }
    }
    

    有关inject 的更多信息,您可以查看this reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 2023-01-03
      • 1970-01-01
      • 2020-06-30
      • 2020-07-28
      • 2013-08-06
      • 2017-05-10
      • 2013-06-12
      相关资源
      最近更新 更多