• uniapp中UView中 u-form表单在v-for循环下如何进行表单校验


    1、数据data格式
    注:rule绑定的tableFromRule中要和表单tableFrom下面放置一个同名数组,确保u-form能找到

    tableFrom: {
    					tableData: [
    						//数据详情列表
    					]
    				},
    				tableFromRule: {
    					//校验
    					tableData: [
    						//数据详情列表
    					]
    				},
    				formRules:{
    					localation:[
    						{
    							required: true,
    							message: '请填写xxxx',
    							trigger: ['blur', 'change']
    						}
    					]
    				},
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2、dom结构

    
    		
    					xxxxxxx
    		
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、u-form-item格式必须用 :prop=“tableData.${index}.localation

    
         xxxxx
    
    
    • 1
    • 2
    • 3

    4、修改源码

    在这里插入图片描述

    在这里插入图片描述
    找到async validateField(value, callback, event = null)函数进行替换
    5、替换如下

    			// 对部分表单字段进行校验
    			async validateField(value, callback, event = null) {
    				// $nextTick是必须的,否则model的变更,可能会延后于此方法的执行
    				this.$nextTick(() => {
    					
    					// 校验错误信息,返回给回调方法,用于存放所有form-item的错误信息
    					const errorsRes = [];
    					// 如果为字符串,转为数组
    					value = [].concat(value);
    					// 历遍children所有子form-item
    					this.children.map((child) => {
    				 
    						// 用于存放form-item的错误信息
    						const childErrors = [];
    						if (value.includes(child.prop)) {
    							// 获取对应的属性,通过类似'a.b.c'的形式
    							const propertyVal = uni.$u.getProperty(
    								this.model,
    								child.prop
    							);
    							
    							// 属性链数组
    							const propertyChain = child.prop.split(".");
    							const propertyName =
    								propertyChain[propertyChain.length - 1];
    							//修改:将const改为let 
    							let rule = this.formRules[child.prop];
    							//修改:链式是无法通过上面的方式获取的,改为下面的方式
    							if(!rule){
    								rule=uni.$u.getProperty(
    								this.formRules,
    								child.prop
    								);
    							
    							}
    							// 如果不存在对应的规则,直接返回,否则校验器会报错
    							if (!rule) return;
    							// rule规则可为数组形式,也可为对象形式,此处拼接成为数组
    							const rules = [].concat(rule);
     
    							// 对rules数组进行校验
    							for (let i = 0; i < rules.length; i++) {
    								const ruleItem = rules[i];
    								
    								// 将u-form-item的触发器转为数组形式
    								const trigger = [].concat(ruleItem?.trigger);
    								
    								// 如果是有传入触发事件,但是此form-item却没有配置此触发器的话,不执行校验操作
    								if (event && !trigger.includes(event)) continue;
    								// 实例化校验对象,传入构造规则
    								const validator = new Schema({
    									[propertyName]: ruleItem,
    								});
    									
    								validator.validate({
    										[propertyName]: propertyVal,
    									},
    									(errors, fields) => {
    									
    										if (uni.$u.test.array(errors)) {
    											errorsRes.push(...errors);
    											childErrors.push(...errors);
    										}
    										child.message =
    											childErrors[0]?.message ?? null;
    									}
    								);
    							}
    						}
    					});
    					// 执行回调函数
    					typeof callback === "function" && callback(errorsRes);
    				});
    			},
    			// 校验全部数据
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    6、在tableData每次塞数据的时候,执行如下代码

    this.tableFromRule.tableData.unshift(this.formRules)
    
    • 1
  • 相关阅读:
    互联网摸鱼日报(2023-10-12)
    2023年最佳研发管理平台评选:哪家表现出色?
    应用统计专业学习指南
    线上数据监测,可以监测哪些平台
    推荐三个搭建专有知识库+大模型智能助手开源项目
    会议AISTATS(Artificial Intelligence and Statistics) Latex模板参考文献引用问题
    基于C++实现的语音信号的处理与滤波
    无涯教程-JavaScript - ERFC.PRECISE函数
    Linux基础命令
    信息系统基础选择题真题
  • 原文地址:https://blog.csdn.net/weixin_41463944/article/details/132625105