验证对象中的文本字段
Validate text field in object
我有一个对象数组,比如显示 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 之间(float 0.1,0.2,..),
记录:在 0 到 100 之间,
感谢您的帮助
此处为两个字段添加了验证: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: ''
});
},
},
})
我有一个对象数组,比如显示 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 之间(float 0.1,0.2,..), 记录:在 0 到 100 之间,
感谢您的帮助
此处为两个字段添加了验证: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: ''
});
},
},
})