鍍金池/ 問答/HTML/ Element UI自定義表單組件怎么加入驗證功能

Element UI自定義表單組件怎么加入驗證功能

自定義了一個表單組件,但是想用上驗證功能不知道如何下手了,求大佬幫忙看看。
這是我定義的控件file-choose,然后用rules不生效

<el-form-item label="選擇目錄" prop="filePath">
  <file-choose v-model="form.filePath"></file-choose>
</el-form-item>

rules: {
  filePath: [
    {required: true, message: '不能為空'}
  ]
},

這是控件的效果圖,現(xiàn)在就是想實現(xiàn)對輸入框的內(nèi)容進行校驗

clipboard.png

控件的代碼

<template>
  <div>
    <el-button type="primary" class="file-choose-button" @click="visible=true">選擇</el-button>
    <el-input class="file-choose-input" :value="value" @dblclick.native="visible = true"></el-input>
    <el-dialog title="目錄瀏覽" :visible="visible" @close="visible = false">
      <el-tree :data="files"
               :props="props"
               :load="loadChild"
               :highlight-current="true"
               node-key="path"
               lazy
               ref="tree"
               class="file-choose-tree"></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary"
                   @click="visible = false;$emit('input',$refs.tree.getCurrentNode().path)">確 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Vue from 'vue'

  export default {
    data() {
      return {
        visible: false,
        files: [],
        props: {
          isLeaf: 'last'
        }
      }
    },
    props: ['value', 'model'],
    methods: {
      loadChild(node, resolve) {
        this.$http.post('api/getChildDirList',
          {
            path: node.data.path || '',
            model: this.model || 'dir',
          })
        .then((response) => {
          let result = response.data;
          if (result.status == 200) {
            if (result.data) {
              resolve(result.data);
            }
          } else {
            this.$message(result.msg);
          }
        });
      }
    }
  }
</script>

要怎么加上驗證的功能呢?

回答
編輯回答
敢試

解決了,研究了下ele-input的代碼發(fā)現(xiàn)會自動找父節(jié)點elFormItem和elForm,所有并不需要特殊處理。
主要是自定義控件的v-model實現(xiàn)有問題,加上@input傳遞值給子控件就好了

<el-input class="file-choose-input" :value="value" @dblclick.native="visible = true" @input="$emit('input',arguments[0])></el-input>
2018年4月30日 15:14