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>
相关推荐
孤水寒月3 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀3 小时前
html初学者第一天
前端·html
耶啵奶膘6 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
视频砖家6 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689976 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽8 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头8 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全9 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing9 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆9 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试