uniapp form表单校验

带required的就是有校验;name要对应model里的值,要统一

复制代码
<template>
	<view class="form-view">
		<uni-forms :modelValue="formData" ref="uniForm" :rules="rules">
			<uni-forms-item label="时间:" name="date" required>
				<uni-datetime-picker type="datetime" return-type="timestamp" v-model="formData.date" placeholder="请选择日期"/>
			</uni-forms-item>
			<uni-forms-item label="名称:" name="name" required>
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入点名称"/>
			</uni-forms-item>
			<uni-forms-item label="行政区:" name="region">
				<uni-easyinput type="text" v-model="formData.region" placeholder="请输入行政区" />
			</uni-forms-item>
			<uni-forms-item label="信息:" name="content">
				<uni-easyinput type="text" v-model="formData.content" placeholder="请输入信息" />
			</uni-forms-item>
		</uni-forms>
		<button style="max-width: 120px;" type="primary" @click="submitForm">提 交</button>
	</view>
</template>

<script>
	export default{
		name:"patrol",
		data(){
			return{
				formData:{
					id:'',
					date:'',
					name:'',
					region:'',
					content:'',
					isAdd:true,// 是否新增
				},
				rules:{
					//对date字段进行必填验证
					date:{
						rules:[
							{required: true,errorMessage: '请选中日期',},
						]
					},
					// 对name字段进行必填验证
					name: {
						rules: [
							{required: true,errorMessage: '请输入点名称',},
							{minLength: 1,maxLength: 5,errorMessage: '名称长度在 {minLength} 到 {maxLength} 个字符',}
						]
					},
				}
			}
		},
		onload(option){// 接收传递过来的值
			
		},
		methods:{
			// 提交按钮
			submitForm(){
				// 调用校验方法
				this.$refs.uniForm.validate().then(res=>{
					console.log('表单数据信息:', res);
					console.log("formData.id",this.formData.id);
					console.log("formData.date",this.formData.date);
					console.log("formData.name",this.formData.name);
				}).catch(err=>{
					console.log('表单数据错误信息:', err);
				})
				
			},
		}
	}
</script>

<style lang="scss" scoped>
.form-view {
  justify-content: center;
  align-items: center;
  flex-direction: column;
  min-height: 100vh;
  margin-left: 20px;
  margin-right: 20px;
}

</style>
相关推荐
wangruofeng2 小时前
AI Models 1.1.0 发布,查大模型价格、对比模型和找 API 地址
前端·aigc·ai编程
wangruofeng2 小时前
给前端开发者的 100 个设计参考网站
前端·设计
星栈5 小时前
视频即代码,Remotion 能做什么、不能做什么
前端·音视频开发
广州华水科技5 小时前
单北斗GNSS位移监测系统的应用与技术解析
前端
用户47949283569156 小时前
从一句话或代码仓到可交互 HTML:开源 Agent 画图技能 Archify v3.2 实测与选型边界
前端·后端·面试
风骏时光牛马8 小时前
生成一个AI多模态标题,仅输出一条标题,无额外文字、问句、备选、配图、文案等任何附加内容,不需要其他的任务服务 只要标题。
前端
wangruofeng8 小时前
从 GIF 到 AVIF:网页图片格式演进与 2026 年选型指南
前端·架构
清晨Coding9 小时前
为什么用 React Developer Tools 查看组件树这么乱?
前端·react.js
鶴哥只手遮天10 小时前
《Electron 45.2.3 内核解剖》第 4 章 补丁体系:380 个补丁改了什么
前端·electron
FungLeo12 小时前
成为全栈·Vue 管理后台篇·分类树与标签管理:层级数据如何落到管理界面
前端·javascript·vue.js·分类树·naive ui·标签管理·前端数据结构