我有.js文件来验证我的表单。我需要检查数据库以查找字段是否重复。为此,使用远程。它总是显示错误消息。我也不知道如何检查响应。
字段:
<input required="true" th:field="*{deptCode}" id="code" type="text" maxlength="3" class="form-control" name="deptCode" placeholder="Example: 014" />
JS:
$('#registrationForm').bootstrapValidator({
fields: {
deptCode: {
message: 'The Department Code is not valid',
validators: {
notEmpty: {
message: 'The Department Code is required and cannot be empty'
},
remote: {
message: 'This Department Code Already Exist',
url: '/path/existingdepartmentcode',
data: function(validator) {
return {
deptcode: validator.getFieldElements('deptCode').val()
};
}
},
callback: {
message: 'The Department Code should not start or end with empty spaces.',
callback: function(value, validator, $field) {
console.log(validator);
if (value === '') {
return true;
}
return true;
}
}
}
}
}
}
});
我的控制器类方法:
@RequestMapping(value = "/path/existingdepartmentcode", method = RequestMethod.GET)
public @ResponseBody boolean checkCompanyHRIS(@RequestParam("deptcode") String deptcode,
RedirectAttributes redirectAttributes, Model model) {
LOGGER.info("existingdepartment" + deptrepo.getDepartmentByCode(deptcode));
return deptrepo.getDepartmentByCode(deptcode) != null;
}
但是它不起作用。请帮帮我!