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>
相关推荐
CoderYanger20 分钟前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_26 分钟前
10个css更新
前端·css
倚栏听风雨1 小时前
npm命令详解
前端
用户47949283569151 小时前
为什么我的react项目启动后,dom上的类名里没有代码位置信息
前端·react.js
键盘飞行员1 小时前
Vue3+TypeScript项目中配置自动导入功能,遇到了问题需要详细的配置教程!
前端·typescript·vue
han_1 小时前
前端高频面试题之Vue(初、中级篇)
前端·vue.js·面试
一枚前端小能手1 小时前
📜 `<script>`脚本元素 - 从加载策略到安全性与性能的完整指南
前端·javascript
掘金安东尼2 小时前
TypeScript为何在AI时代登顶:Anders Hejlsberg 的十二年演化论
前端·javascript·面试
yong99902 小时前
MATLAB倍频转换效率分析与最佳匹配角模拟
开发语言·前端·matlab
面向星辰2 小时前
扣子开始节点和结束节点
java·服务器·前端