根据您提供的代码和需求,我进行了一些修改和补充,以下是修正后的代码:
const trimLeft = function trimLeft(s) {
return s.replace(/(^\s*)/g, '');
};
const isEmpty = function isEmpty(value) {
return value === undefined || value === null || trimLeft(value) === '';
};
const checkRequired = (fields) => {
if (isEmpty(fields['extension']) || isEmpty(fields['maxExtenPeriods']) || isEmpty(fields['repayChannel'])) {
return '都要填写';
}
};
const checkoutLegalPerson = (field, value) => {
if (!value) {
return `${field}不能为空`;
}
};
const validator = createValidator({
extension: [(value, fields) => {
if (fields.extension && isEmpty(value)) { // 判断是否需要填写最长展期期数
return '最长展期期数不能为空';
}
}],
maxExtenPeriods: [(value, fields) => checkoutLegalPerson('maxExtenPeriods', value)],
repayChannel: [(value, fields) => checkoutLegalPerson('还款渠道', value)],
});
// 使用示例:
// 假设字段的初始值为:
let repayChannel = { offline: true, active: false, withhold: false };
// 在提交前进行校验
let errors = {};
errors.extension = validator.extension(repayChannel.extension);
errors.maxExtenPeriods = validator.maxExtenPeriods(repayChannel.maxExtenPeriods);
errors.repayChannel = validator.repayChannel(repayChannel.repayType);
console.log(errors); // 打印错误信息
// 如果有任何一个字段的错误信息存在,则验证不通过
const isValid = Object.values(errors).every(error => error === undefined);
console.log(isValid); // 打印验证结果
上述代码对校验函数进行了修正,同时添加了一些缺失的逻辑,并假设字段的初始值为repayChannel = { offline: true, active: false, withhold: false }。您可以根据实际情况调整初始值和使用方式,以满足您的需求。
内容由零声教学AI助手提供,问题来源于学员提问




