uniapp 表单使用Uview校验 包括城市选择器

html 复制代码
<view>
			<!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="174">

				<u-form-item label="身份选择" prop="userInfo.sex" borderBottom @click="showSex = true; " ref="item1">
					<u--input v-model="model1.userInfo.sex" disabled disabledColor="#ffffff" placeholder="请选择身份"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="真实姓名" prop="userInfo.name" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.name" border="none" placeholder="请输入您的姓名"></u--input>
				</u-form-item>
				<u-form-item label="所在城市" prop="userInfo.city" borderBottom @click="show1 = true; " ref="item1">
					<u--input v-model="model1.userInfo.city" disabled disabledColor="#ffffff" placeholder="请选择所在城市"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item label="执业机构" prop="userInfo.jigou" borderBottom ref="item1">
					<u--input v-model="model1.userInfo.jigou" border="none" placeholder="请输入您的所在机构"></u--input>
				</u-form-item>
			</u--form>
			<u-action-sheet :show="showSex" :actions="actions" @close="showSex = false"
				@select="sexSelect">
			</u-action-sheet>
		
			<view class="">
				<u-picker :show="show1" ref="uPicker" :columns="cityList" @confirm="cityConfirm" @cancel="cancel()" itemHeight="70"
					@change="changeHandler"></u-picker>
			</view>
		</view>
javascript 复制代码
<script>
	import cityData from '@/utils/city.js'
	export default {
		data() {
			return {
				showSex: false,
				model1: {
					userInfo: {
						name: '',
						sex: '',
						city: '',
						jigou:''
					},
				},
				actions: [{
						name: '医生',
					},
					{
						name: '员工',
					},

				],
				cityList: [],
				cityLevel1: [],
				cityLevel2: [],
				cityLevel3: [],
				areaname:'',
				show1: false,
				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'userInfo.sex': {
						type: 'string',
						max: 2,
						required: true,
						message: '请选择身份',
						trigger: ['blur', 'change']
					},
					'userInfo.jigou': {
						type: 'string',
						required: true,
						message: '请填写机构',
						trigger: ['blur', 'change']
					},
				},
				radio: '',
				switchVal: false
			};
		},
		onReady() {
			//如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad() {
			// 城市选择器初始化
			this.initCityData();
		},
		methods: {
			sexSelect(e) {
				console.log(e)
				this.model1.userInfo.sex = e.name
				this.$refs.uForm.validateField('userInfo.sex')
			},
			citySelect(e) {
				console.log(e)
				this.model1.userInfo.city = e.name
				this.$refs.uForm.validateField('userInfo.city')
			},
			// 城市选择器
			initCityData() {
				// 遍历城市js
				cityData.forEach((item1, index1) => {
					let temp2 = [];
					this.cityLevel1.push(item1.provinceName);
					let temp4 = [];
					let temp3 = [];
					// 遍历市
					item1.cities.forEach((item2, index2) => {
						temp2.push(item2.cityName);
						// 遍历区
						item2.counties.forEach((item3, index3) => {
							temp3.push(item3.countyName);
						})
						temp4[index2] = temp3;
						temp3 = [];
					})
					this.cityLevel3[index1] = temp4;
					this.cityLevel2[index1] = temp2;
				})
				// 选择器默认城市
				this.cityList.push(this.cityLevel1, this.cityLevel2[0], this.cityLevel3[0][0]);
				console.log(this.cityList)
			}, // 选中时执行
			changeHandler(e) {
				const {
					columnIndex,
					index,
					indexs,
					value,
					values,
					// 微信小程序无法将picker实例传出来,只能通过ref操作
					picker = this.$refs.uPicker
				} = e;
				if (columnIndex === 0) { // 选择第一列数据时
					// 设置第二列关联数据
					picker.setColumnValues(1, this.cityLevel2[index]);
					// 设置第三列关联数据
					picker.setColumnValues(2, this.cityLevel3[index][columnIndex]);
				} else if (columnIndex === 1) { // 选择第二列数据时
					// 设置第三列关联数据
					picker.setColumnValues(2, this.cityLevel3[indexs[0]][index]);
				}
			},
			// 单击确认按钮时执行
			cityConfirm(e) {
				// 输出数组 [省, 市, 区]
				console.log(e.value);
				this.areaname = e.value
				this.cityName = e.value.join("-");
				// 隐藏城市选择器
				console.log(this.cityName);
				this.show1 = false;
				this.model1.userInfo.city = this.cityName
			},
			cancel() {
				this.show1 = false
			},
		},
	}
</script>
html 复制代码
	/deep/ .u-popup__content {
		width: 100%;
		height: 800rpx;
	}

	/deep/.u-icon__icon {
		color: #1FCFC9 !important;
		font-size: 32rpx !important;
	}

	/deep/ .u-action-sheet__item-wrap button {
		background: #fff !important;
	}

	/deep/ .u-action-sheet__item-wrap button::after {
		border: none;
	}

	/deep/.u-form {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 690rpx;
		margin: auto;
		margin-top: 48rpx;

		.u-form-item {
			width: 100%;
		}

		
	}
	/deep/ .u-form-item__body {
		width: 100%;
		height: 120rpx !important;
		display: flex;
		align-items: center;
		justify-content: space-between;
	
		
	
	}
	/deep/ .u-input__content__field-wrapper__field {
		color: #333 !important;
		font-family: YouSheShaYuFeiTeJianKangTi !important;
		font-size: 28rpx !important;
	}
		/deep/ .u-form-item__body__left__content__label {
			color: #333 !important;
			font-family: YouSheShaYuFeiTeJianKangTi !important;
			font-size: 28rpx !important;
		}

city.js资源已上传

相关推荐
baozhengw1 小时前
UniAPP快速入门教程(一)
前端·uni-app
公子艾5 小时前
uniapp设置tabBar高斯模糊并设置tabBar高度占位
uni-app
晓风伴月7 小时前
uniapp: IOS微信小程序输入框部分被软键盘遮挡问题
微信小程序·小程序·uni-app
转角羊儿7 小时前
uni-app文章列表制作⑧
前端·javascript·uni-app
糊涂涂是个小盆友8 小时前
前端 - 使用uniapp+vue搭建前端项目(app端)
前端·vue.js·uni-app
阿巴阿巴Ovo9 小时前
uniapp中多角色导致tabbar过多的解决方式
uni-app
Json____12 小时前
学法减分交管12123模拟练习小程序源码前端和后端和搭建教程
前端·后端·学习·小程序·uni-app·学法减分·驾考题库
云深时现月13 小时前
jenkins使用cli发行uni-app到h5
前端·uni-app·jenkins
diygwcom14 小时前
低代码可视化-uniapp开关选择组件-低码生成器
uni-app
fakaifa1 天前
CRMEB Pro版v3.1源码全开源+PC端+Uniapp前端+搭建教程
前端·小程序·uni-app·php·源码下载