基於vue2.0前端組件庫element中 el-form表單 自定義驗證填坑


eleme寫的基於vue2.0的前端組件庫: http://element.eleme.io

我在平時使用過程中,遇到的問題。

自定義表單驗證出坑:

1: validate/resetFields 未定義。

原因:

1:要驗證的DOM,還沒有加載出來。
2:有可能this.$refs[ruleForm].validate() 方式不識別。需要使用: this.$refs.ruleForm.validate(); 這種方式,不是你們想要的結果。

解決方法

1: 要驗證的DOM,還沒有加載出來。

this.ticketDialog = true;

//對整個表單進行重置,將所有字段值重置為初始值並移除校驗結果

this.$nextTick(function() {

  this.$refs.ticketInfoForm.resetFields();

})

2: 有可能this.$refs[ruleForm].validate() 方式不識別。需要使用: this.$refs.ruleForm.validate();

methods: {   

  submitForm(ruleForm2) {

  	//官網 this.$refs[ruleForm2].validate();  

    //在實際使用中,會報錯。validate未定義

    //使用this.$refs.ruleForm2.validate(); 成功。

    this.$refs[ruleForm2].validate((valid) => {

      if (valid) {

        alert('submit!');

      } else {

        console.log('error submit!!');

        return false;

      }

    });

  }

}

2: 數字類型的驗證, 兼容mac和windows系統。

數字類型的驗證需要在 v-model 處加上 .number 的修飾符,這是 Vue 自身提供的用於將綁定值轉化為 number 類型的修飾符。

<el-form-item label="年齡" prop="age">
	<el-input type="number" v-model.number="ruleForm2.age"></el-input>
</el-form-item>

案例

例子在 codepen 可以查看

html:

<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui/lib/index.js"></script>
<div id="app">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="密碼" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item label="確認密碼" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" auto-complete="off"></el-input>
  </el-form-item>
  <el-form-item label="年齡" prop="age">
    <el-input type="number" v-model.number="ruleForm.age"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>

css:

@import url("//unpkg.com/element-ui/lib/theme-default/index.css");

js:

	var Main = {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年齡不能為空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('請輸入數字值'));
          } else {
            if (value < 18) {
              callback(new Error('必須年滿18歲'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('請輸入密碼'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('請再次輸入密碼'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('兩次輸入密碼不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(ruleForm) {
        //官網 this.$refs[ruleForm].validate();  
        //在實際使用中,會報錯。validate未定義
        // 
        //使用this.$refs.ruleForm.validate(); 成功。
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$nextTick(function() {
          this.$refs[formName].resetFields();
         })
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

See the Pen ele-form-numrules by LiuwqGit (@weiqinl) on CodePen.

總結

先看官方文檔,一部好的文檔,會解決大部分問題。遇到具體問題,認真仔細看相關部分的內容。

歡迎留言討論
vuejs后台管理項目


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM