uniapp 中使用uview表单验证时,自定义扩展的表单,在改变时无法触发表单验证处理;

uniapp中使用uview表单时,自定义扩展的表单,无法及时触发uview表单验证,但是在提交的时候,表单又可以进行表单验证。其中的原因是uview触发机制所决定的。处理方法:需要在自定义扩展表单组件内部加入一个触发方法,触发on-form-change或者on-form-blur事件,这两个在rules里面对应 trigger:change 或 blur;(注意触发机制需要放在微任务或宏任务中,避免验证触发混乱),下面以on-form-change该事件为例子:

第一步: 在自定义扩展组件内部加入以下代码:(此代码在表单改变的时候触发,val为改变的结果值,后面会以自定义日期组件为例子,贴上完整代码)

javascript 复制代码
onChangeFormItemValid(val) {
			this.$nextTick(() => {
				this.dispatch('u-form-item', 'on-form-change', val);
			});
		},

2、完成之上步骤,应该可以看到dispatch方法报错,那是因为我们的代码中没有这个方法。这个方法需要用到uview表单中的公共方法,使用如下:在自定义扩展表单组件中引入 Emitter,并使用

mixins: [Emitter]: 如图:

最后附上代码,供参考:

javascript 复制代码
<template>
	<view class="mn-calendar">
		<view class="select-input" @click="!disabled && (show = true)">
			<template v-if="$slots.default || $scopedSlots.default">
				<slot :title="getTitle"></slot>
			</template>
			<template v-else>
				{{ getTitle || '请选择' }}
				<template v-if="!disabled">
					<view class="clear-icon" @click.stop="onClear" v-show="getTitle"><u-icon name="close"
							size="28"></u-icon>
					</view>
					<view class="arrow-icon"><u-icon name="arrow-right" size="38" color="#999"></u-icon></view>
				</template>
			</template>
		</view>
		<u-calendar v-model="show" :mode="mode" @change="onChange" :max-date="maxDate"></u-calendar>
	</view>
</template>

<script>
import moment from 'js/moment.js'
import Emitter from '@/uview-ui/libs/util/emitter.js';
export default {
	name: "mn-calendar",
	mixins: [Emitter],
	props: {
		value: String | Array,
		mode: {
			type: String,
			default: 'date'
		},
		disabled: {
			type: Boolean,
			default: false
		},
		maxDate: {
			type: String,
			default: moment().add(1, 'year').format('YYYY-MM-DD')
		}
	},
	data() {
		return {
			text: '',
			show: false
		};
	},
	computed: {
		getTitle() {
			if (!this.value) return ''
			if (this.mode === 'range') {
				return this.value.length == 2 ? `${this.value[0]} ~ ${this.value[1]}` : ''
			} else {
				return this.value
			}
		},
	},
	methods: {
		onChange(e) {
			if (this.mode === 'range') {
				const { startDate, endDate } = e
				this.$emit('input', [startDate, endDate])
			} else {
				this.$emit('input', e.result)
			}
			this.onEmitChange()
		},
		onEmitChange() {
			this.$emit('change', this.value)
			this.$nextTick(() => {
				this.dispatch('u-form-item', 'on-form-change', this.value);
			});
		},
		onClear() {
			if (this.mode === 'range') {
				this.$emit('input', [])
			} else {
				this.$emit('input', '')
			}
			this.onEmitChange()
		}
	}
}
</script>

<style lang="scss" scoped>
.mn-calendar {
	width: 100%;
}

.select-input {
	width: 100%;
	box-sizing: border-box;
	padding-right: 78rpx;
	padding-left: 0;
	position: relative;

	.clear-icon {
		position: absolute;
		right: 38rpx;
		padding: 20rpx;
		top: 50%;
		transform: translateY(-50%);
	}

	.arrow-icon {
		position: absolute;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
	}
}
</style>

就可以按照u-input一样进行使用了,为了扩展性更强。

相关推荐
琹箐6 分钟前
最大堆和最小堆 实现思路
java·开发语言·算法
黑色的糖果8 分钟前
vue中tailwindcss插件的引入及使用
前端·javascript·vue.js
戌中横12 分钟前
JavaScript——预解析
前端·javascript·学习
Monly2139 分钟前
Java:修改打包配置文件
java·开发语言
AALoveTouch43 分钟前
大麦网协议分析
javascript·python
●VON1 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
我命由我123451 小时前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
island13141 小时前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
木斯佳1 小时前
前端八股文面经大全:26届秋招滴滴校招前端一面面经-事件循环题解析
前端·状态模式
xcLeigh1 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests