Commit ec9bbd0b by chenwq

优化表单验证逻辑,支持只校验已填字段

1 parent 42230f6f
......@@ -143,9 +143,10 @@ Component({
/**
* 表单验证方法
* @param {Object} rules 验证规则,格式同 firstData.js 中的 formRule
* @param {Boolean} filledOnly 是否只校验已填字段,默认 false
* @returns {Object|null} 返回错误信息 {field, message, attr} 或 null
*/
validate(rules) {
validate(rules, filledOnly = false) {
const { formData } = this.data;
if (!rules || typeof rules !== "object") {
......@@ -166,6 +167,11 @@ Component({
for (let j = 0; j < fieldRules.length; j++) {
const rule = fieldRules[j];
// 如果只校验已填字段,且字段为空,则跳过 required 检查
if (filledOnly && !value) {
continue;
}
// 必填校验
if (rule.required && !value) {
// 特殊处理:对于地区ID字段,如果有对应的名称字段,则认为有效
......
......@@ -269,7 +269,7 @@ Component({
return thisPage?.data[this.data.dataAttrName];
},
// 表单验证方法
validate() {
validate(onlyFilled = false) {
if (this.data.vinValid === false) {
let msg = "车架号校验未通过,请重新输入";
wx.showToast({ title: msg, icon: 'none' });
......@@ -278,7 +278,7 @@ Component({
var { formRule } = this.data;
var serviceInfo = this.getPageData() || this.data.serviceInfo;
let validObj = fn.checkRequired(serviceInfo, formRule);
let validObj = fn.checkRequired(serviceInfo, formRule, onlyFilled);
if (validObj) {
wx.showToast({ title: validObj.message, icon: 'none' });
return validObj;
......
......@@ -77,11 +77,11 @@ Component({
console.log('timeChange', item, value);
},
validate() {
validate(onlyFilled = false) {
const { formRule } = this.data;
const thisPage = wx.p.router.getPageInstance();
const serviceInfo = (thisPage && thisPage.data[this.data.dataAttrName]) || this.data.serviceInfo;
const errorObj = fn.checkRequired(serviceInfo, formRule);
const errorObj = fn.checkRequired(serviceInfo, formRule, onlyFilled);
if (errorObj) {
wx.showToast({
title: errorObj.message,
......
......@@ -51,11 +51,11 @@ Component({
inputBlur(e) {
},
validate() {
validate(onlyFilled = false) {
const { formRule } = this.data;
const thisPage = wx.p.router.getPageInstance();
const serviceInfo = (thisPage && thisPage.data[this.data.dataAttrName]) || this.data.serviceInfo;
const errorObj = fn.checkRequired(serviceInfo, formRule);
const errorObj = fn.checkRequired(serviceInfo, formRule, onlyFilled);
if (errorObj) {
wx.showToast({
title: errorObj.message,
......
......@@ -2,6 +2,7 @@
* 动态校验字段
* @param {*} obj 需校验的对象
* @param {*} formRule 校验规则
* @param {*} onlyFilled 是否只校验已填字段
* formRule = {
* id: [
* { required: true, message: 'xx不能为空' },
......@@ -13,7 +14,7 @@
* ],
* }
*/
const checkRequired = function (obj, formRule) {
const checkRequired = function (obj, formRule, onlyFilled = false) {
let target = Object.keys(formRule)
for (let i = 0; i < target.length; i++) {
......@@ -21,20 +22,37 @@ const checkRequired = function (obj, formRule) {
let item = formRule[attr]
let requirObj = item.find(k => k.required == true)
let validFn = item.find(k => k.validator)
if (requirObj && !obj[attr]) {
return {
...requirObj,
attr
if (onlyFilled) {
// 只校验已填字段
if (obj[attr] != null && obj[attr] !== '') {
if (validFn) {
let validObj = validFn.validator({}, obj[attr])
if (validObj) {
return {
...validObj,
attr
}
}
}
}
}
if (validFn) {
let validObj = validFn.validator({}, obj[attr])
if (validObj) {
} else {
// 正常校验
if (requirObj && (obj[attr] == null || obj[attr] === '')) {
return {
...validObj,
...requirObj,
attr
}
}
if (validFn) {
let validObj = validFn.validator({}, obj[attr])
if (validObj) {
return {
...validObj,
attr
}
}
}
}
}
return false
......
......@@ -180,16 +180,38 @@ Page({
// },
handlePrevStep() {
// 第四步时,上一步不需要做校验,直接返回上一页
if (this.data.currentStep === 4) {
this._goToStep(this.data.currentStep - 1);
return;
}
// 添加校验:返回上一步前只校验当前步骤已填的值
const stepComp = this.selectComponent("#" + this.data.step.charAt(0).toUpperCase() + this.data.step.slice(1) + "Step");
if (stepComp) {
const errorObj = stepComp.validate(true); // true 表示只校验已填字段
if (errorObj) {
return;
}
}
if (this.data.currentStep > 1) {
const prevNum = this.data.currentStep - 1;
const stepMap = { 1: "first", 2: "second", 3: "third", 4: "fourth" };
this.setData({
currentStep: prevNum,
step: stepMap[prevNum],
});
this._goToStep(this.data.currentStep - 1);
}
},
/**
* 切换到指定步骤
* @param {number} stepNum 步骤编号 (1-4)
*/
_goToStep(stepNum) {
const stepMap = { 1: "first", 2: "second", 3: "third", 4: "fourth" };
this.setData({
currentStep: stepNum,
step: stepMap[stepNum],
});
},
handleNextStep() {
if (this.data.currentStep === 1) {
this.firstStepSubmit();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!