非空验证方法(多值)
function save() {//保存记录
checkBlank([['type_name','类型名称'],['type_code','类型标识']]);
if(!isNumber($("#type_code").val())){
$("#type_code_td").append("[类型标识]必须为数字!")
count++;
return false;
}
if(count>0){
count=0;
return false;
}
toAction('${webPath}/dicttype/insert')
}
checkBlank(arr)方法为非空验证方法
function checkBlank(arr) {
$.each(arr, function(key, val) {
notBlank(val[0], val[1]);
});
}
var count = 0;
function notBlank(id, name) {
$("#" + id + "_td").html("");
if ($("#" + id).val() == "") {
$("#" + id + "_td").html("["+name + "]不能为空!");
count++;
}
}
checkBlank方法中传入的参数为二维数组,通过$.each()方法进行遍历。
BindingResult提示验证信息
/**
* @功能描述 共用的验证
*/
private void validate(DictType dictType, BindingResult result, int pos) {
DictTypeCond cond = new DictTypeCond();
cond.setType_code_c(dictType.getType_code());
if (pos == 2) {
cond.setId_c(dictType.getId());
}
// 条件:类型标识,主键<>
int count = service.queryCount(cond);
if (count > 0) {
result.rejectValue("type_code", "", "类型标识不能重复!!!");
}
}
其中的pos=2只在修改记录时使用,其他情况下pos为不等于2的任意值。
当pos=2时会在DictTypeCond中加入当前记录的id值,在后面的queryCount(cond)方法中会在sql语句后拼接and id <> ?,用于在查找类型标识是否重复时,排除当前记录。