需求背景 ISV目前在使用低代码开发时,经常有一些前端校验的业务功能,以前大多数只能高代码定制实现,现在平台提供了hooks能力(前面简单的js脚本)就可以实现,下面是其中关于hooks能力中的数据重复性校验的功能使用样例
实现方式
数据重复性检查校验
1、同步规则重复性检查只有表格可用(检查当前字段是否有重复数据-当前展示数据)

//id为表格id
var table = component.getComponentById("e0a94d0a-0b42-fe51-d1fa-93fea2717391")
var valuesum = 0
for (var i = 0; i < table.getAllRowData().length; i++) {
//获取的product_no为当前添加校验栏位的字段
var tablevalue = table.getAllRowData()[i].value.product_no;
if (value === tablevalue) { valuesum++ }
}
if (valuesum > 1) {
component.getControl().setErrors({
repeat: {
key: 'repeat',
type: 'error',
errorMessage: '数据重复',
},
})
}
2、异步规则表单、表格都可使用(检查该字段数据是否有重复数据-接口中所有数据)

// 获取编号值
const novalue = parentData.contract_no
// 请求esp请求入参---请求contract_no字段
const parameter = {
"zh_contract": [
{
"contract_no": novalue
}
]
}
// 发送esp请求
options.utils.espApi.getEspByActionId({
actionId: "dtdapp.zh.contract.list.get",
parameter,
}).subscribe((res) => {
// 打印接口返回结果
console.log('res', res)
if(res.data.zh_contract.length > 0){
component.getControl().setErrors({
customInvalid: {
key: 'customInvalid',
type: 'error',
errorMessage: '编号重复',
},
});
}
}, () => { });