qingcheng-xiaochengxu/utils/validate.js

19 lines
545 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// utils/validate.js
/**
* 通用非空校验
* @param {Object} data - 当前页面的 this.data
* @param {Object} rules - 校验规则,格式:{ fieldName: '提示文案', ... }
* @returns {boolean} - 校验通过返回 true失败时已经 wx.showToast 提示并返回 false
*/
function validate(data, rules) {
for (const [field, message] of Object.entries(rules)) {
if (!data[field]) {
wx.showToast({ title: message, icon: 'none' });
return false;
}
}
return true;
}
module.exports = { validate };