表单验证不生效

vue

element-ui

需要注意

:model

:rules

ref

html 复制代码
<el-form :model="myFormData" status-icon :rules="rules" ref="myFormData" label-width="100px" class="demo-ruleForm">

  <el-form-item label="姓名" prop="name">
    <el-input type="text" v-model="myFormData.name"></el-input>
  </el-form-item>
  
  <el-form-item label="年龄" prop="age">
    <el-input type="text" v-model="myFormData.age"></el-input>
  </el-form-item>
  
  <el-form-item>
    <el-button type="primary" @click="submitForm()">提交</el-button>
  </el-form-item>
  
</el-form>
js 复制代码
<script>
  export default {
    data() {
      return {
        myFormData: {
          name: '',
          age: ''
        },
        rules: {
        	name:[
        		{ 
        			required: true, 
        			message: '请输入姓名', 
        			trigger:['blur','change']
        		}
        	],
        	age:[
        		{ 
        			required: true, 
        			message: '请输入年龄', 
        			trigger:['blur','change']
        		}
        	],
        }
      };
    },
    methods: {
      submitForm() {
      	this.$refs['myFormData'].validate((valid) => {
			if (valid) {
				// 执行表单验证成功该执行的代码
			} else {
				// 执行表单验证失败应该执行的代码
			}
		});
        
      },
    }
  }
</script>

uniapp

uni-forms

需要注意

:modelValue

:rules

ref

html 复制代码
<template>
	<view>
		<div class="all">
			<view class="homework">
			
				<uni-forms :modelValue="myFormData" :rules="rules" ref="myFormData">
				
					<uni-forms-item label="姓名:" name="realName" :required="true">
						<uni-easyinput type='text' v-model="myFormData.realName" placeholder="请输入姓名" />
					</uni-forms-item>
					
					<uni-forms-item label="身份证号码" prop="" name="idNumber" :required="true">
						<uni-easyinput v-model="myFormData.idNumber" placeholder="请输入身份证号码" />
					</uni-forms-item>
					
				</uni-forms>
				
			</view>
	
			<div class="openDevice">
				<view class="btn" @click="submit">
					提交
				</view>
			</div>
		</div>
	</view>
</template>
js 复制代码
<script>
	export default {
		data() {
			return {
				myFormData: {
	                realName:'',
	                idNumber:''
	            },
				rules: {
					realName: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						},
						// {
						// 	minLength: 3,
						// 	maxLength: 5,
						// 	errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
						// }
					]},
					idNumber: {
						rules: [{
							required: true,
							errorMessage: '请输入身份证号码',
						},
					]},
				},
			}
		},
		onLoad(options) {},
	    onShow() {},
		mounted() {},
		methods: {
			submit() {
				this.$refs['myFormData'].validate((valid) => {
					console.log(valid);
				    if (!valid) {
						// 验证通过
				        console.log('验证通过')
				        this.$emit('tabComponent', this.info);
				        ElMessage.success("注册成功!")
				 
				    } else {
						// 验证不通过
						console.log('验证不通过')
					}
				})
	            
	            console.log(this.myFormData)
			},
		}
	}
</script>
css 复制代码
<style scoped lang="scss">
	.all {
		width: 100vw;
		background-color: #fff;
		// padding: 30rpx;
		box-sizing: border-box !important;

		.news {
			width: 100vw;
			height: 513rpx;
			background: url('../../../static/scan/homeworkBg.png') no-repeat;
			background-size: 100% 100%;
			background-attachment: cover;
			background-color: #37a7f1;
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			padding-left: 80rpx;
			color: #fff;
			box-sizing: border-box;
		}

		.homework {
			padding: 70rpx;
			box-sizing: border-box;

			overflow: hidden;

		}

		.box {
			position: relative;
			top: -30px;
			z-index: 999;
			background: #fff;
			width: 100%;
			border-radius: 40rpx 40rpx 0rpx 0rpx;
		}

		::v-deep .uni-forms-item__label {
			width: 120px !important;
			color: #333333;
			font-weight: 500;
			font-size: 30rpx;
		}

		::v-deep .uni-forms-item {
			border-bottom: 1px solid #f4f3f3;
		}

		::v-deep .uni-select {
			border: none !important;
		}

		::v-deep .is-input-border {
			border: none !important;
		}
        .list-cell{
            display: flex;
        }
        .uni-list-cell{
            margin-right: 10rpx;
        }
		.btn {
			width: 80%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			background: #3F9DFD;
			border-radius: 50rpx;
			font-weight: 400;
			font-size: 32rpx;
			color: #FFFFFF;
		}

		.openDevice {
			width: 100%;
			display: flex;
			justify-content: center;
		}

		.text {
			width: 427rpx;
			height: 160rpx;
			font-family: Noto Sans S Chinese;
			font-weight: normal;
			font-size: 28rpx;
			color: #333333;
		}
	}
</style>
相关推荐
Mintopia5 分钟前
B 样条曲线:计算机图形学里的 “曲线魔术师”
前端·javascript·计算机图形学
前端小巷子8 分钟前
跨域问题解决方案:CORS(跨域资源共享)
前端·网络协议·面试
大大。9 分钟前
van-tabbar-item选中active数据变了,图标没变
java·服务器·前端
Mintopia11 分钟前
Three.js 3D 世界中的噪声运动:当数学与像素共舞
前端·javascript·three.js
nc_kai12 分钟前
Flutter 之 每日翻译 PreferredSizeWidget
java·前端·flutter
来碗疙瘩汤14 分钟前
使用 Three.js 与 CSS3DRenderer 在 Vue3 中加载网页为 3D 模型
前端·javascript
满分观察网友z16 分钟前
富文本解析终极指南:从Quill到小程序,我如何用正则摆平所有坑?
前端
打野赵怀真16 分钟前
在TypeScript中装饰器有哪些应用场景?
前端·javascript
destinying18 分钟前
vite学习笔记
前端·javascript
LRH19 分钟前
JS基础 - 手写数组扁平化函数
前端·javascript