校验明细必填且明细内某些字段不能为空
场景
报销场景,通常报销明细是不能为空的,且报销时,报销时发票和报销金额必填,报销金额不能超过发票金额,我们就可以用个性化开发 来验证报销明细内的数据是否符合条件。
模板配置

个性化代码块示例代码
<script type="text/javascript">
cf.ready(function () {
// 提交前校验发票和报销金额
cf.addEventHook(cf.EVENT_HOOKS.BEFORE_SUBMIT, function (next) {
// 获取明细控件值
var detailWidgetValue = cf.form.getFieldValue('Dd_0')
// 如果明细控件有值
if (detailWidgetValue && detailWidgetValue.widgetValue) {
var widgetValue = detailWidgetValue.widgetValue
var errMsg = ""
if (widgetValue.length === 0) {
errMsg = "报销明细-至少应该包含一行明细"
} else {
// 遍历每行明细值做判断
for (var i = 0; i < widgetValue.length; i++) {
var item = widgetValue[i]
// 发票控件
if (item.At_0.length === 0) {
errMsg = "报销明细-必须上传发票"
break
}
// 报销金额
if (item.Mo_0 == "") {
errMsg = "报销明细-报销金额不能为空"
break
}
}
}
// 如果有错误 则提示用户 阻止表单提交
if(errMsg) {
cf.call('alert', { content: errMsg, onlyAlert: true })
next(false)
} else {
// 无错误 表单继续提交
next()
}
} else {
// 允许用户继续提交
next()
}
})
})
</script>
运行时效果


欢迎关注我的个人公众号「「小枫学幽默」」一起成长,一起分享生活!!