element-ui 自定義表單驗證 , 可是不出現小紅心了

基本上按照文檔上提供的方式作就沒啥大問題 , 我遇到的問題是 , 自定義之後不顯示小紅星了ui

<el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
  <el-form-item label="密碼" prop="pass">
    <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="確認密碼" prop="checkPass">
    <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="年齡" prop="age">
    <el-input v-model.number="ruleForm2.age"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
    <el-button @click="resetForm('ruleForm2')">重置</el-button>
  </el-form-item>
</el-form>
<script>
  export default {
    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.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('請再次輸入密碼'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('兩次輸入密碼不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          checkPass: '',
          age: ''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    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();
      }
    }
  }
</script>

  我只是照着改了一下this

let validatePass = (rule, value, callback) => {
      console.log(rule);
      console.log(value);
      console.log(callback);
      if (!value) {
        return callback(new Error("請填寫公司名稱"));
      }
      if (this.form.id) {
        callback();
        return true;
      }
      readScmSupplierPage({ name: this.form.name ,type:'2'})
        .then(res => {
          if (res.data.length > 0) {
            callback(new Error("名稱重複"));
          } else {
            callback();
          }
        })
        .catch(err => {
          console.log(err);
        });
    };

  基本上和自定義沒啥關係orm

rules: {
        // name: [{ required: true, message: "請輸入公司名稱", trigger: "blur" }],
        name: [{ required: true, validator: validatePass, trigger: "blur" }],
        abbreviation: [
          { required: true, message: "請輸入公司簡稱", trigger: "blur" }
        ]
      },

  只是我發現若是自定義了 , 在這個地方加required: true, 是不起做用的, 必須在form表單上面加blog

<el-form-item label="公司名稱" :label-width="formLabelWidth" prop="name" required>
          <el-input v-model="form.name"></el-input>
        </el-form-item>

  就這樣小紅星星就出現啦ip

相關文章
相關標籤/搜索