我有一个使用收音机交换选择列表的表单,但验证消息似乎无法正常工作。这是我的表单,TypeA 验证消息确实有效:
但是当我将单选按钮更改为 TypeB 时,验证消息不起作用:
并且如果我单击提交按钮并且如果 TypeA 验证不正确并且我更改为 TypeB 提交它,则验证将不会通过,因为它看起来像 vee-validate 仅验证了 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>
我不知道如何解决这个问题,有人可以帮助我吗?