element-ui里,对于通过v-for渲染的el-form-item组件,使用自带的表单验证

<el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
  <el-form-item
    prop="email"
    label="邮箱"
    :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
    ]"
  >
    <el-input v-model="dynamicValidateForm.email"></el-input>
  </el-form-item>
  <el-form-item
    v-for="(domain, index) in dynamicValidateForm.domains"
    :label="'域名' + index"
    :key="domain.key"
    :prop="'domains.' + index + '.value'"
    :rules="{
      required: true, message: '域名不能为空', trigger: 'blur'
    }"
  >
    <el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">删除</el-button>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
    <el-button @click="addDomain">新增域名</el-button>
    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
  </el-form-item>
</el-form>
<script>
  export default {
    data() {
      return {
        dynamicValidateForm: {
          domains: [{
            value: ''
          }],
          email: ''
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
      addDomain() {
        this.dynamicValidateForm.domains.push({
          value: '',
          key: Date.now()
        });
      }
    }
  }

</script>

<div class="content-inner" :key="index" v-for="(experience, index) in resumeData.workExperiences" >
    <el-form-item :prop="'workExperiences.' + index + '.company'" label="公司" label-position="top" :rules="rules.experienceRule.company">
        <el-input class="input-item" v-model="experience.company"></el-input>
    </el-form-item>
</div>
export const rules = () => {
    return {
        experienceRule: {
            company: [
                { required: true, message: '公司名不能为空', trigger: 'blur' },
                { min: 1, max: 128, message: '不能超过 128 个字符', trigger: 'change, blur' },
                {  validator: validDangeroutStr, trigger: 'change, blur' }  //validator为自定义校验
            ]
        }
    }
}


阅读更多
换一批

没有更多推荐了,返回首页