鍍金池/ 問答/Linux  HTML/ 關(guān)于element表單驗證

關(guān)于element表單驗證

在el-form中加入了表單驗證為必填字段,但在表單為空的時候點擊保存按鈕,底下報錯Cannot read property 'validate' of undefined"

<el-dialog title="新增角色" :visible.sync="dialogAdd"  width="30%">

  <el-form label-position="right" ref="roleAdd" label-width="80px" :model="roleAdd" :rules="addRules">
    <el-form-item label="角色名稱" prop="RoleName">
      <el-input v-model="roleAdd.RoleName" placeholder="請輸入"></el-input>
    </el-form-item>

    <el-form-item label="角色描述">
      <el-input v-model="roleAdd.RoleRemark"  type="textarea"></el-input>
    </el-form-item>
  </el-form>

  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="create(roleAdd)">保 存</el-button>
    <el-button @click="dialogAdd = false">取 消</el-button>
  </span>
</el-dialog>

addRules: {
      RoleName: [
        { required: true, message: '請輸入部門名稱', trigger: 'blur' },
      ],
  },
  
create(formName) {
  this.$refs[formName].validate(valid => {
    if (valid) {
      const param = {
        PKID: 0,
        RoleName: this.roleAdd.RoleName,
        RoleRemark: this.roleAdd.RoleRemark
      };
      api.addRoleInfo(param).then(response => {
        if (response.data.Success) {
          this.$notify({
            title: "成功",
            message: "創(chuàng)建成功",
            type: "success",
            duration: 2000
          });
          this.dialogShow = false;
          this.getList();
        } else {
          this.$notify({
            title: "失敗",
            message: response.data.Message,
            type: "error",
            duration: 2000
          });
        }
      });
      this.getList();
    } else {
      return false;
    }
  });
},
  

具體是什么原因,需要怎么解決

回答
編輯回答
風(fēng)清揚

@click="create(roleAdd)"
改為
@click="create('roleAdd')"

2017年2月24日 23:05
編輯回答
氕氘氚

this.$refs[formName].validate
應(yīng)改為
this.$refs.roleAdd.validate

拷貝粘貼功夫不到家啊 ^_^

2018年6月17日 15:22
編輯回答
獨特范

你輸出一下this.$refs[formName]是什么

2017年7月22日 19:54