首页 文章

Vue.js - 使用ve-validate无法使用v-for正确验证自定义单个文件组件

提问于
浏览
3

我've been building a vue app and I'当前正在构建一个具有"Person"对象的组件,以及一些输入字段来添加有关该人的信息,例如姓名,地址,电子邮件等 . 其中一个字段是手机号码,但一个人可以拥有更多而不是一个数字,所以我创建了一个自定义组件,可以随意重复输入字段 . 这大致就是它的样子 . 所有这些输入都使用vee validate进行验证 . 这大概是这样的:

<!--createPerson.vue -->
     <div class="form-group">
        <input v-model="person.firstName" v-validate="'required'" data-vv-delay="500" name="first-name" type="text" placeholder="First name">
        <span v-show="errors.has('first-name')" class="input__error">{{ errors.first('first-name') }}</span>
    </div>

    <div class="form-group">
        <input v-model="person.lastName" v-validate="'required'" data-vv-delay="500" name="last-name" type="text" placeholder="Last name">
        <span v-show="errors.has('last-name')" class="input__error">{{ errors.first('last-name') }}</span>
    </div>

    <repeatable-inputs v-model="person.mobiles"  v-validate="'required'" data-vv-value-path="input" data-vv-name="mobile" type="tel" name="mobile" placeholder="Mobile" :inputmode="numeric" link-name="mobile number"></repeatable-inputs>

    <div class="form-group">
        <input v-model="person.email"  v-validate="'required|email'" data-vv-delay="500" name='email' type="text" placeholder="Personal email">
        <span v-show="errors.has('email')" class="input__error">{{ errors.first('email') }}</span>
    </div>

person对象在同一个createPerson.vue文件下有一个名为mobiles的属性,它以一个空数组开头 . 它还有一个validateAll()函数,如vee-validate文档中所述,在单击“下一步”按钮时执行所有输入的验证 . 然后,在repeatableInputs.vue上,有这样的:

<template>
    <div>
        <div class="form-group" v-for="(input, index) in inputs">
            <input
                :type="type"
                :name="name+index"
                :placeholder="placeholder"
                :inputmode="inputmode"
                :value="input"
                @input="update(index, $event)"
                v-focus></input>

        </div>

        <a href="#" v-on:click.prevent="add">Add another {{linkName}}</a>


    </div>
    </template>

    <script>
        export default {
            props: {
                value: {
                    type: Array,
                    default: ['']
                },
                type: {
                    type:       String,
                    default:    "text"
                },

                name:           String,
                placeholder:    String,
                inputmode:      String,

                linkName: {
                    type:       String,
                    default:    "input"
                }
            },

            data: function () {
                return {
                    inputs: this.value
                }
            },
            methods: {
                add: function () {
                    this.inputs.push('');
                },

                update: function(index, e) {
                    this.inputs[index] = e.target.value;
                    this.$emit('input', this.inputs);
                }
            }
        }
    </script>

为了能够使用validateAll()函数在父createPerson.vue中验证这些自定义输入,文档声明我必须引用 Should have a data-vv-value-path attribute which denotes how to access the value from within that component (Needed for validateAll calls) .

这就是我被卡住的地方 . 我不确定那个vv-path必须指向,我尝试使用'value','input','inputs',创建一个watch函数,以及一些甚至没有多大意义的其他东西 . 我发现了一个git问题,用于验证使用v-for的自定义输入,但显然已经修复了 .

1 回答

  • 1

    Data-vv-value-path应指向数据在组件状态内的位置 . 在输入上,您可以使用v-model -attribute将输入内容绑定到数据,然后验证程序知道它需要验证的子组件的哪个属性 .

    因此,data-vv-value-path指向子组件内的数据,并且当与v-model绑定时,数据将自动更新 .

相关问题