zoukankan      html  css  js  c++  java
  • element 表单验证

    其实element 中 Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名即可。校验规则参见 async-validator

    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/element-ui@2.10.1/lib/index.js"></script>
    <div id="app">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
    <el-form-item
    prop="email"
    label="邮箱"
    :rules="[
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
    >
    <el-input v-model="dynamicValidateForm.email"></el-input>
    </el-form-item>
    <el-form-item
    v-for="(domain, index) in dynamicValidateForm.domains"
    :label="'域名' + index"
    :key="domain.key"
    :prop="'domains.' + index + '.value'"
    :rules="{
    required: true, message: '域名不能为空', trigger: 'blur'
    }"
    >
    <el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">删除</el-button>
    </el-form-item>
    <el-form-item>
    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
    <el-button @click="addDomain">新增域名</el-button>
    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
    </el-form-item>
    </el-form>
    </div>

    <script>
     

    var Main = {
    data() {
    return {
    dynamicValidateForm: {
    domains: [{
    value: ''
    }],
    email: ''
    }
    };
    },
    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();
    },
    removeDomain(item) {
    var index = this.dynamicValidateForm.domains.indexOf(item)
    if (index !== -1) {
    this.dynamicValidateForm.domains.splice(index, 1)
    }
    },
    addDomain() {
    this.dynamicValidateForm.domains.push({
    value: '',
    key: Date.now()
    });
    }
    }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app')

    </script>

     

  • 相关阅读:
    Linux Kernel Makefiles Kbuild en
    Android 源码结构分析
    Linux Charger IC 驱动移植总结
    Battery Charging Specification Revision 1.2 中文版本
    OpenCV 3.4.2 Windows系统下的环境搭建(附带opencv_contrib-3.4.2)
    OpenCV 经纬法将鱼眼图像展开
    shell 循环结构
    OpenCV之Mat类使用总结
    shell 条件结构之 if 语句使用总结
    OpenCV Error: Unspecified Error(The Function is not implemented)
  • 原文地址:https://www.cnblogs.com/yearshar/p/11170808.html
Copyright © 2011-2022 走看看