表单验证
Form组件提供了表单验证的功能,只需要通过 rules
属性传入约定的验证规则,并将Form-Item的prop属性设置为需校验的字段名即可
<el-form-item label="活动名称" prop="name">
<el-form :model="ruleForm" :rules="rules" ref="ruleForm">
注1:有多个表单,怎么在提交进行区分?
我们在rules这里写了对表单的验证规则,但是我们如何在methods里进行指定的表单进行认证, 所以我们一开始就在el-form里写了 ref=“ruleForm”,我们在methods里就可以用
注2:清空表单验证信息
this.$refs[formName].resetFields();
代码吧:
<template>
<div style="padding: 20px;">
<!-- 面包导航 -->
<el-breadcrumb separator-class="el-icon-arrow-right">
<!-- 这里可用写跳转的路径 -->
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>文章管理</el-breadcrumb-item>
</el-breadcrumb>
<!-- 搜索筛选框 -->
<el-form :inline="true" class="user-search">
<el-form-item label="搜索:">
<!-- 和title进行了双向数据绑定 -->
<el-input size="small" v-model="title" placeholder="文章标题"></el-input>
</el-form-item>
<el-form-item>
<!-- 当点击搜索的时候会调用 search方法 -->
<el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
<el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd()">添加</el-button>
</el-form-item>
</el-form>
<!-- 列表 -->
<el-table size="small" :data="listData" style="width: 100%;">
<el-table-column align="center" type="selection" width="60">
</el-table-column>
<!-- prop 代表数据列段名 -->
<el-table-column sortable prop="id" label="文章ID" min-width="1">
</el-table-column>
<el-table-column sortable prop="title" label="文章内容" min-width="3">
</el-table-column>
<el-table-column sortable prop="body" label="文章内容" min-width="6">
</el-table-column>
<el-table-column label="操作" min-width="2">
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<el-button size="mini" type="danger" @click="articlesDelete(scope.$index, scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<el-pagination style="margin-top: 20px;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:current-page="pageBean.page" :page-sizes="[10, 20, 30, 50]" :page-size="100" layout="total, sizes, prev, pager, next, jumper"
:total="pageBean.total">
</el-pagination>
<!--
articleDialogFormVisible 决定了弹出是否展示
doCancel 关闭弹出方法
articleForm 这是实现双向数据绑定
rules 绑定表单验证对应的配置对象
ref 指定了当前表单的引用
doSubmit 提交方法 -->
<!-- 新增修改通用弹出 -->
<el-dialog :title="articleTitle" :visible="articleDialogFormVisible" @close="doCancel">
<el-form :model="articleForm" :rules="articleRules" ref="articleForm">
<el-form-item label="文章标题" :label-width="articleFormLabelWidth" prop="title">
<el-input v-model="articleForm.title" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="文章内容" :label-width="articleFormLabelWidth" prop="body">
<el-input type="textarea" v-model="articleForm.body" rows="6"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="doCancel">取 消</el-button>
<el-button type="primary" @click="doSubmit">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'Articles',
data: function() {
return {
title: null,
listData: [],
pageBean: {
page: 1,
rows: 10,
total: 0
},
articleTitle: null,
articleDialogFormVisible: false,
articleFormLabelWidth: '120px',
articleForm: {
id: null,
title: null,
body: null
},
//表单验证配置规则对象
articleRules: {
//这里的验证规则时,当第一个验证规则通过那么就会进入到第二个验证规则,所有规则通过验证成功
title: [{
required: true,
message: '文章标题不能为空',
trgger: 'blur' //当焦点离开时验证
}, {
min: 3,
max: 10,
message: '长度在 3 到 10 个字符',
trgger: 'blur'
}],
body: [{
required: true,
message: '文章内容不能为空',
trigger: 'change' //当内容改变的验证
}]
}
}
},
methods: {
//搜索方法
search: function() {
let url = this.axios.urls.SYSTEM_ARTICLE_LIST;
var formData = {
page: this.pageBean.page,
rows: this.pageBean.rows,
title: this.title
}
//向后端请求数据
this.axios.post(url, formData).then(response => {
this.listData = response.data.result
this.pageBean = response.data.pageBean;
}).catch(function(error) {
console.log(error);
});
},
//这是当一页展示数据数量变化的时候的回掉函数
handleSizeChange: function(rwos) {
this.pageBean.page = 1;
this.pageBean.rows = rwos;
this.search();
},
//当当前页该改变的时候调用
handleCurrentChange: function(page) {
this.pageBean.page = page;
this.search();
},
//添加前期调用方法,打开太弹出
handleAdd: function() {
this.articleDialogFormVisible = true;
},
//修改前期调用方法,给表单回显对应数据 并打开弹出
handleEdit: function(id, row) {
this.articleForm.id = row.id;
this.articleForm.title = row.title;
this.articleForm.body = row.body;
this.articleDialogFormVisible = true;
},
//删除调用方法
articlesDelete: function(id, row) {
this.$confirm('你确定要哦删除吗?', '警告', {
//定义两个按钮 确定和取消
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//row.id就是要删除行的id
this.articleForm.id = row.id;
var url = this.axios.urls.SYSTEM_ARTICLE_DEL;
this.axios.post(url, this.articleForm).then(response => {
if (response.data.code == 0) {
this.$message({
message: response.data.msg,
type: 'warning'
});
} else {
this.$message({
message: response.data.msg,
type: 'success'
});
this.search();
}
}).catch(function(error) {
console.log(error);
});
}).catch(() => {});
},
//这是提交就调用的方法【添加、修改通用】
doSubmit: function() {
//this.$refs['articleForm'].validate就是表单验证是否通过,
//valid为true即为通过
this.$refs['articleForm'].validate((valid) => {
if (valid) {
var url = null;
//判断时添加还是修改
if (null == this.articleForm.id) {
url = this.axios.urls.SYSTEM_ARTICLE_ADD;
} else {
url = this.axios.urls.SYSTEM_ARTICLE_EDIT;
}
//发送请求
this.axios.post(url, this.articleForm).then(response => {
if (response.data.code == 0) {
this.$message({
message: response.data.msg,
type: 'warning'
});
} else {
this.doClearForm();
this.articleDialogFormVisible = false;
this.search();
this.$message({
message: response.data.msg,
type: 'success'
});
}
}).catch(function(error) {
console.log(error);
});
} else {
console.log('error submit!!');
return false;
}
});
},
//清空表单是数据
doClearForm: function() {
this.articleForm.id = null;
this.articleForm.title = null;
this.articleForm.body = null;
},
//清空表单数据,并且关闭弹出
doCancel: function() {
this.doClearForm();
this.articleDialogFormVisible = false;
}
},
created: function() {
this.search();
}
}
</script>
<style scoped>
.user-search {
margin-top: 20px;
}
.userRole {
width: 100%;
}
</style>