JQuery.validate校验不能重复的问题

var validator;
var isExistence;
$(document).ready(function() {
    jQuery.validator.addMethod("isExistence", function(value, param) {
        $.ajax({
            url : '${ctx}/***/isExistence',
            method : 'post',
            data : {
                id : $("#id").val(),
                materialCode : $("#materialCode").val()
            },
            dataType : 'json',
            async : false,
            success : function(data) {
                isExistence = data;
            }
        })
        return !isExistence;
    }, $.validator.format("物料编码已存在"));

    validator = $("#searchForm").validate({
        rules : {
            materialCode : {
                isExistence : true
            }
        }
    })
})

后台对填写的数据进行校验:

@RequestMapping("/isExistence")
@ResponseBody
public Boolean isExistence(HttpServletRequest request, MmpMaterialDefDto mmpMaterialDefDto) {
    Boolean isExistence = false;
    try {
        if(!StringUtils.isEmpty(mmpMaterialDefDto.getMaterialCode())){
            isExistence = materialDefService.isExistence(mmpMaterialDefDto);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isExistence;
}

你可能感兴趣的:(jquery.validate)