Commit ec9bbd0b by chenwq

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

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