1

我有一个对象数组,例如显示 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 之间,

感谢您的帮助

4

1 回答 1

2

这里对这两个字段添加了验证: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: ''
              });
            },
  },
})
于 2019-10-24T17:57:07.417 回答