目录
一、表单验证
1.1 实现表单验证的步骤
① 编写表单组件 el-form
② 编写点击事件 打开添加文章的窗体
③ 给表单设置规则 rules
④ 在表单提交时校验规则
1.2 参考代码
1. 表单组件
<!-- 编辑界面 -->
<el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @before-close="closeDialog">
<el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
<el-form-item label="文章标题" prop="title">
<el-input size="small" v-model="editForm.title" auto-complete="off" placeholder="请输入文章标题"></el-input>
</el-form-item>
<el-form-item label="文章内容" prop="body">
<el-input size="small" v-model="editForm.body" auto-complete="off" placeholder="请输入文章内容"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button size="small" @click="closeDialog">取消</el-button>
<el-button size="small" type="primary" class="title" @click="submitForm('editForm')">保存</el-button>
</div>
</el-dialog>
我们写好表单后还需要把用到的属性,定义到data中。
2. 打开窗体和关闭窗体的方法
handleEdit() {
//打开编辑窗体
this.editFormVisible = true;
},
closeDialog() {
//关闭窗体
this.editForm = false;
},