赞
踩
el-form需要的属性
ref=“ruleFormRef” :model=“ruleForm” :rules=“rules”
显示效果:
详细代码:
<template> <div id="Content-page"> <div class="contetn-main"> <!-- 表单 --> <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px"> <el-form-item label="账户" prop="account"> <el-input v-model="ruleForm.account" type="password" autocomplete="off" /> </el-form-item> <el-form-item label="密码" prop="password"> <el-input v-model="ruleForm.password" type="password" autocomplete="off" /> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button> <el-button @click="resetForm(ruleFormRef)">重置</el-button> </el-form-item> </el-form> </div> </div> </template> <script setup> import { ref,reactive } from 'vue'; import {ElForm,ElFormItem,ElInput,ElButton} from 'element-plus' const ruleFormRef = ref() //表单数据对象 const ruleForm = reactive({ account: '', password: '', }) //表单校验 const rules = reactive({ account: [{ required: true, message: '请输入账户', trigger: 'blur' }], password: [ {required: true,message: '请输入密码',trigger: 'blur'}, { min: 6, max: 8, message: '长度为 6 to 8', trigger: 'blur' } ] }) //表单数据提交事件 const submitForm = (formEl) => { if (!formEl) return formEl.validate((valid) => {//对表单进行一个校验,接收一个回调函数,返回promise if (valid) { console.log('校验成功ok!') } else { console.log('校验失败error!') return false } }) } //表单数据重置事件 const resetForm = (formEl) => { if (!formEl) return formEl.resetFields() } </script> <style lang="scss" scoped></style>
注意:
失败效果图及代码演示:
成功效果图及代码演示:
2.表单校验,需满足自己编写的rules的表单属性情况
required表示必填
提交表单进行校验,执行submitForm事件,方法判断校验是否都通过rules
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。