1. 程式人生 > >Element 表單驗證,取消部分表單欄位校驗結果

Element 表單驗證,取消部分表單欄位校驗結果

<template>
  <div>
    <el-form ref="formRules" :model="form" :rules="rules" :inline="true">
      <el-form-item prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item prop="age" ref="ageForm">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="validate">驗證</el-button>
        <el-button type="primary" @click="cancelFieldValidate">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    name: `clearFieldValidate`,
    data () {
      return {
        formRules: '',
        form: {
          name: '',
          age: ''
        },
        rules: {
          name: [
            { required: true, message: '請輸入姓名!', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '請輸入年齡!', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      validate () {
        this.$refs.formRules.validate(valid => {
          if (valid) {
            this.$message.success('(^o^)~ 驗證成功!')
          } else {
            this.$message.error('-_- 驗證失敗!')
          }
        })
      },
      cancelFieldValidate () {
        this.$refs['ageForm'].clearValidate() // 清除年齡的驗證
      }
    }
  }
</script>

原博:https://blog.csdn.net/IH1107/article/details/79174137