Layui Form 自定義驗證
1.前臺頁面
<div class="layui-form-item">
<label class="layui-form-label">BOM編號</label><div class="layui-input-block">
<input type="text" class="layui-input" name='code'
lay-verify="required|unique">
</div>
</div>
2.前臺Js
需要引用layui.js檔案
<script type="text/javascript">
var form = layui.form;
form.verify({
'unique' : function(value, item) {
var checkValue = value.trim();
var checkResult='';
$.ajax({
url : '/bomManage/checkBomCode',
type : 'POST',
data : {
"newBomCode" : checkValue
},
datatype : 'json',
async : false,
success : function(result) {
if (!result) {
checkResult='BOM編號重複'
}
},
error : function() {
kok.message("BOM編號驗證失敗")
}
});
return checkResult
}
})
</script>
3.後臺邏輯處理
@Override
public boolean checkBomCode(String newBomCode) {
BdBomVersionExample example=new BdBomVersionExample();
Criteria criteria = example.createCriteria();
criteria.andCodeEqualTo(newBomCode.trim());
List<BdBomVersion> checks = bdBomVersionMapper.selectByExample(example);
boolean checkValue=true;
if(checks.size()>0){
checkValue=false;
}
return checkValue;
}