我有一个对象数组,例如谁显示2个时间字段并记录:
sprint:[{time: void 0, record: void 0}]
<div v-for="(s, index) in sprint" :key="index">
<v-text-field
class="input"
v-model="sprint.time"
></v-text-field>
<v-text-field
v-model="sprint.record"
></v-text-field>
</div>
当我单击添加按钮时,我有一个函数可以在以下位置添加这些字段:
addSprint() {
this.sprint.push({
time: '',
record: ''
});
},
我想使用Vuelidate来检查每个是否存在。 验证必须是字段 需要, 数字 时间:介于0和1之间(浮动0.1,0.2,..), 记录:0到100之间,
感谢您的帮助
答案 0 :(得分:1)
在这两个字段中都添加了验证:https://codepen.io/chansv/pen/eYYWGLe?editors=1010
<div id="app">
<v-app id="inspire">
<v-form>
<v-container>
<div v-for="(s, index) in sprint" :key="index">
Index: {{index}}
<v-text-field
class="input"
label="time"
:rules="[rules.required, rules.time]"
v-model="s.time"
></v-text-field>
<v-text-field
label="record"
:rules="[rules.required, rules.record]"
v-model="s.record"
></v-text-field>
</div>
<v-btn @click="addSprint">Add Sprint</v-btn>
</v-container>
</v-form>
</v-app>
</div>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: {
sprint: [],
rules: {
required: v => !!v || 'this field is required',
time: v => (v >= 0 && v <= parseFloat(1)) || "enter value less than 1 or greater than 0",
record: v => (v >= 0 && v <= 100) || "enter value less than 100 or greater than 0",
}
},
methods: {
addSprint() {
this.sprint.push({
time: '',
record: ''
});
},
},
})