vue+element多层表单校验prop和rules

核心点:外层循环是item和index,内层循环是item2和index2

如果都是定义的同一个属性名
外层循环得写:prop="'block.'+index+'.numerical'"
同理内层循环就得写:prop="'objectSpecs.'+ index2 + '.numerical'"

校验函数方法 :rules="getRules(item2, item2.name)"

<div class="block" v-for="(item, index) in block" :key="index">
  <el-form ref="blockForm" :model="item" :rules="formBlockRule" :inline="true">
    <el-form-item>多个formItem...省略一下el-form-item>
    <div v-for="(item2,index2) in item.objectSpecs" :key="index2">
      
      <el-form-item v-if="item2.input" 
                    :prop="'objectSpecs.' + index2 + '.numerical'" 
                    :label="item2.name" label-width="130px" 
                    :rules="getRules(item2, item2.name)">
        <el-input :placeholder="item2.placeholder" 
                  v-model="item2.numerical" 
                  :onkeyup="numberFormate(item2, 'numerical')" 
                  :disabled="readOnly">
        el-input>
      el-form-item>
      <el-form-item v-if="item2.input">
        <span>{{item2.units}}span>
      el-form-item>
      
      <el-form-item v-if="!item2.input" 
                    :prop="'objectSpecs.' + index2 + '.numerical'" 
                    :label="item2.name" label-width="130px" 
                    :rules="getRules(item2, item2.name)">
        <el-select v-model="item2.numerical" 
                   placeholder="请选择" 
                   :disabled="readOnly">
          <el-option v-for="(key,value) in item2.numericalList" 
                     :key="value" 
                     :label="key" 
                     :value="value">
          el-option>
        el-select>
      el-form-item>
    div>
  el-form>
div>
getRules(item, name) {
  if(item.input){
    return [
      { required: true, message: '请输入' + name, trigger: 'blur' },
    ]
  }else{
    return [
      { required: true, message: '请选择' + name, trigger: 'change' }
    ];
  }
}

你可能感兴趣的:(vue.js,elementui)