add form validation demo

This commit is contained in:
jonkee 2017-11-16 20:51:19 +08:00
parent 925dbeb045
commit 5aece3ce7e
3 changed files with 114 additions and 2 deletions

View File

@ -20,8 +20,10 @@
"echarts": "3.8.2", "echarts": "3.8.2",
"element-ui": "1.4.2", "element-ui": "1.4.2",
"file-saver": "1.3.3", "file-saver": "1.3.3",
"hakim": "^0.2.5",
"js-cookie": "2.1.4", "js-cookie": "2.1.4",
"jsonlint": "1.6.2", "jsonlint": "1.6.2",
"jszip": "^3.1.4",
"mockjs": "1.0.1-beta3", "mockjs": "1.0.1-beta3",
"normalize.css": "7.0.0", "normalize.css": "7.0.0",
"nprogress": "0.2.0", "nprogress": "0.2.0",
@ -36,8 +38,7 @@
"vue-splitpane": "^1.0.0", "vue-splitpane": "^1.0.0",
"vuedraggable": "2.14.1", "vuedraggable": "2.14.1",
"vuex": "2.3.1", "vuex": "2.3.1",
"xlsx": "^0.10.8", "xlsx": "^0.10.8"
"jszip": "^3.1.4"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "7.1.1", "autoprefixer": "7.1.1",

View File

@ -119,6 +119,7 @@ export const asyncRouterMap = [
}, },
{ path: 'form/edit', icon: 'form', component: _import('example/form'), name: '编辑Form', meta: { isEdit: true }}, { path: 'form/edit', icon: 'form', component: _import('example/form'), name: '编辑Form', meta: { isEdit: true }},
{ path: 'form/create', icon: 'form', component: _import('example/form'), name: '创建Form' }, { path: 'form/create', icon: 'form', component: _import('example/form'), name: '创建Form' },
{ path: 'form/validate', icon: 'form', component: _import('example/validation'), name: '验证Form' },
{ path: 'tab/index', icon: 'tab', component: _import('example/tab/index'), name: 'Tab' } { path: 'tab/index', icon: 'tab', component: _import('example/tab/index'), name: 'Tab' }
] ]
}, },

View File

@ -0,0 +1,110 @@
<template>
<div>
<div class="info">
element-ui自带的表单验证有诸多不足比如一个表单项要么为空要么为整数如何写为了解决这个问题引入hakim这个小工具
可以用<span class="code"></span><span class="code"></span>的方式来组合多条规则灵活方便
https://github.com/zzzgit/hakim
</div>
<div style="width: 400px">
<el-form :model="formModel" :rules="rules" ref="form" label-width="250px">
<el-form-item label="大于0的数字最多1位小数" prop="item1">
<el-input v-model="formModel.item1"></el-input>
</el-form-item>
<el-form-item label="同时包含英文和数字" prop="item2">
<el-input v-model="formModel.item2"></el-input>
</el-form-item>
<el-form-item label="为空,为正整数,或者为电子邮件" prop="item3">
<el-input v-model="formModel.item3"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('form')">校验</el-button>
<el-button @click="resetForm('form')">重置</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script>
import Hakim from 'hakim'
export default {
name: 'articleDetail',
data() {
return {
formModel: {
item1: null,
item2: null,
item3: null
},
rules: {
item1: [
{
required: true,
trigger: 'change',
validator: (rule, value, resolve) => {
if (!new Hakim([{ is: 'number' }, { gt: 0 }, { dplacesLt: 2 }]).validate(value)) {
return resolve(new Error('大于0的数字最多1位小数'))
}
return resolve()
}
}
],
item2: [
{
required: true,
trigger: 'change',
validator: (rule, value, resolve) => {
if (!new Hakim([{ isNot: 'empty' }, { exist: 'latin' }, { exist: 'digit' }]).validate(value)) {
return resolve(new Error('同时包含英文和数字'))
}
return resolve()
}
}
],
item3: [
{
trigger: 'change',
validator: (rule, value, resolve) => {
if (!new Hakim([{ is: 'empty' }, [{ is: 'number' }, { is: 'positive' }, { is: 'integer' }], { is: 'email' }, true]).validate(value)) {
return resolve(new Error('为空,为正整数,或者为电子邮件'))
}
return resolve()
}
}
]
}
}
},
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>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.info{
height: 80px;
padding-top: 20px;
padding-left: 10px;
background-color: gray;
margin-bottom: 20px;
}
.code{
background-color: yellow;
}
</style>