<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

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:2 个月前 )
c345bb45 6 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐