gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:19:23 24 4
gpt4 key购买 nike

我有一个使用 radio 交换选择列表的表单,但验证消息似乎无法正常工作。这是我的表单,TypeA 验证消息确实有效: enter image description here

但是当我将单选按钮更改为 TypeB 时,验证消息不起作用: enter image description here

而且如果我点击提交按钮并且如果 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>

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

最佳答案

使用 v-show 而不是 v-if 来观察变化,因为 v-if 添加/删除 dom 元素和 vee-validate 检查在 DOM 中。

<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>

此外,您还在验证所有字段,这意味着在两个下拉列表都在验证时。

让它发挥作用。意味着一次只验证一个下拉菜单我更改了这一行。来自

this.$validator.validateAll()

this.$validator.validate(this.Type)

关于vue.js - 使用 vee-validate 验证单选按钮交换的选择列表不显示验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com