ithack / ithack.github.io

我的前端博客,访问地址:
https://ithack.github.io
2 stars 1 forks source link

饿了么form组件校验问题记录 #9

Open ithack opened 5 years ago

ithack commented 5 years ago

正常的form校验是model绑定一个对象然后form-item绑定对象下的key这样ok,哪怕之前做的动态循环生成的form表单校验也是用这种解构,今天我有一个校验但是我的form中model绑定的是个数组,这时我想在不改变数据解构情况下绑定校验,通过查资料如下解决方案:

<el-form v-for="(item, index) in list" :key="index" :model="item">
    <el-form-item :label="item.question" prop="answer" :rules="{ required: true, trigger: 'blur' }">
      <el-input v-model="item.answer"></el-input>
    </el-form-item>
  </el-form>

data() {
    return {
      list: [
        {
            question: 'Q1',
          answer: '',
        },
        {
            question: 'Q2',
          answer: '',
        },
        {
            question: 'Q3',
          answer: '',
        },
        {
            question: 'Q4',
          answer: '',
        },
        {
            question: 'Q5',
          answer: '',
        },
        {
            question: 'Q6',
          answer: '',
        },
      ],
    }
  }