使用vee-validate验证选择列表,单选按钮交换不显示验证消息

mar*_*ang 3 vue.js vee-validate

我有表单使用无线电交换选择列表,但验证消息似乎无法正常工作.这是我的表单,TypeA验证消息是有效的: 在此输入图像描述

但是当我将单选按钮更改为TypeB时,验证消息不起作用: 在此输入图像描述

如果我单击提交按钮并且TypeA验证不正确并且我更改为TypeB以提交它,验证将不会通过,因为它看起来只是验证TypeA ...

这是我的代码:

<form id="form" @submit.prevent="validateBeforeSubmit">
    <label>Type A</label>
    <input type="radio" v-model="Type" value="TypeA" />
    <label>Type B</label>
    <input type="radio" v-model="Type" value="TypeB" />

    <table>
        <tr v-if="Type==='TypeA'">
            <td>
                <select v-model="TypeA" v-validate="'required|not_in:Choose'" name="TypeA">
                    <option v-for="option in TypeAOptions" v-bind:value="option.value">
                        {{ option.value }}
                    </option>
                </select>
                <span v-if="errors.has('TypeA')">
                    {{ errors.first('TypeA')}}
                </span>
            </td>
        </tr>
        <tr v-if="Type==='TypeB'">
            <td>
                <select v-model="TypeB" v-validate="'required|not_in:Choose'" name="TypeB">
                    <option v-for="option in TypeBOptions" v-bind:value="option.value">
                        {{ option.value }}
                    </option>
                </select>
                <span v-if="errors.has('TypeB')">
                    {{ errors.first('TypeB')}}
                </span>
            </td>
        </tr>
    </table>
    <button type="submit">Submit</button>
</form>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>

<script>
    Vue.use(VeeValidate);
    var form = new Vue({
        el: '#form',
        data: {
            Type: 'TypeA',
            TypeA: 'Choose',
            TypeAOptions: [{
                    value: 'Choose'
                },
                {
                    value: 'A',
                },
                {
                    value: 'B'
                },
                {
                    value: 'C'
                },
                {
                    value: 'D'
                }
            ],

            TypeB: 'Choose',
            TypeBOptions: [{
                    value: 'Choose'
                },
                {
                    value: '1'
                },
                {
                    value: '2'
                },
                {
                    value: '3'
                },
                {
                    value: '4'
                }
            ],
        },
        methods: {
            validateBeforeSubmit() {
                this.$validator.validateAll().then((result) => {
                    if (result) {
                        alert("Submit Success");
                        return;
                    }
                    alert("Correct them errors!");
                });
            }
        }
    })
</script>
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题,有人可以帮帮我吗?

Nik*_*aut 6

v-show相反v-if,请使用以观察更改,因为v-if在DOM中添加/删除dom元素和vee-validate检查.

<form id="form" @submit.prevent="validateBeforeSubmit">
    <label>Type A</label>
    <input v-on:change="changeType" type="radio" v-model="Type" value="TypeA" />
    <label>Type B</label>
    <input v-on:change="changeType" type="radio" v-model="Type" value="TypeB" />

    <table>
        <tr v-show="Type==='TypeA'">
            <td>
                <select v-model="TypeA" v-validate="'required|not_in:Choose'" name="TypeA">
                    <option v-for="option in TypeAOptions" v-bind:value="option.value">
                        {{ option.value }}
                    </option>
                </select>
                <span v-if="errors.has('TypeA')">
                    {{ errors.first('TypeA')}}
                </span>
            </td>
        </tr>
        <tr v-show="Type==='TypeB'">
            <td>
                <select v-model="TypeB" v-validate="'required|not_in:Choose'" name="TypeB">
                    <option v-for="option in TypeBOptions" v-bind:value="option.value">
                        {{ option.value }}
                    </option>
                </select>
                <span v-if="errors.has('TypeB')">
                    {{ errors.first('TypeB')}}
                </span>
            </td>
        </tr>
    </table>
    <button type="submit">Submit</button>
</form>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vee-validate@latest/dist/vee-validate.js"></script>

<script>
    Vue.use(VeeValidate);
    var form = new Vue({
        el: '#form',
        data: {
            Type: 'TypeA',
            TypeA: 'Choose',
            TypeAOptions: [{
                    value: 'Choose'
                },
                {
                    value: 'A',
                },
                {
                    value: 'B'
                },
                {
                    value: 'C'
                },
                {
                    value: 'D'
                }
            ],

            TypeB: 'Choose',
            TypeBOptions: [{
                    value: 'Choose'
                },
                {
                    value: '1'
                },
                {
                    value: '2'
                },
                {
                    value: '3'
                },
                {
                    value: '4'
                }
            ],
        },
        computed:{
        },
        methods: {
            changeType:function(){
              this.errors.clear();
            },
            validateBeforeSubmit() {
                this.$validator.validate(this.Type).then((result) => {
                    if (result) {
                        alert("Submit Success");
                        return;
                    }
                    alert("Correct them errors!");
                });
            }
        }
    })
</script>
Run Code Online (Sandbox Code Playgroud)

此外,您验证所有字段均意味着两个下拉列表都在验证.

使它工作.意味着一次只验证一个下拉列表我改变了这一行.从

this.$validator.validateAll()
Run Code Online (Sandbox Code Playgroud)

this.$validator.validate(this.Type)
Run Code Online (Sandbox Code Playgroud)