zoukankan      html  css  js  c++  java
  • elementUI表单校验动态添加校验规则

    elementui动态添加校验规则,场景:
    如果活动名称为空,则所有字段非必填
    如果活动名称不为空,则具体活动名称提示必填
     
    <template>
      <div id="app">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="form.name" @blur="blur()"></el-input>
          </el-form-item>
          <el-form-item label="具体活动" prop="name1">
            <el-input v-model="form.name1"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </template>
    
    <script>
    export default {
      name: "app",
      components: {},
      data() {
        return {
          form: {
            name: "",
            name1: "",
          },
          rules: {},
        };
      },
      methods: {
        addRules() {
          // 定义规则
          const newRules = [{ required: true, trigger: "change", message: "具体活动必填" }];
          // 给rules对象添加规则
          this.rules = { ...this.rules, name1: newRules };
        },
        removeRules() {
            // 清除指定校验规则
            this.$refs.form.clearValidate(["name1"]);
          // 这行必须
          this.rules = { ...this.rules, name1: [] };
        },
    
        blur() {
          if (this.form.name === "") {
            this.removeRules();
          } else {
            this.addRules();
          }
        },
      },
    };
    </script>
    

      效果:

    活动名称为空时,具体活动选项非必填

    活动名称不为空时,具体活动选项必填

  • 相关阅读:
    OpenCR 固件修复
    E-PUCK2机器人-固件更新
    E-puck2机器人系列教程-2.软件的安装与使用
    E-PUCK2机器人-硬件
    E-puck2机器人系列教程-固件修复升级
    GridView
    TimePicker 和TimePickerDiag
    android中实现简单的播放
    ListView的使用
    android的activity的跳转
  • 原文地址:https://www.cnblogs.com/luguankun/p/15317633.html
Copyright © 2011-2022 走看看