我正在尝试使用一个自定义验证器验证用户输入,下面是我一直使用的验证器代码。
public void validate(Object target, Errors errors) {
System.out.println("Validating...");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age", "age.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "full_name", "full_name.required");
}
然后我把这个发给我的控制器
@PostMapping(value = "/newUser")
public String newResults(@Valid Volunteer vol, BindingResult result, Model model) {
if(result.hasErrors()) {
model.addAttribute("errors", result.getAllErrors().get(0).hashCode());
return "/sign_up";
}
else {
//other code
我的html页面上出现了545324234这样的代码。然后我用javascript询问它是否是一个特定的数字,然后告诉页面它是这个错误代码。
function check(){
var x = document.getElementById("error2").textContent;
if (x=="1182092568"){
document.getElementById("error").textContent="An Email is required to sign up!";
}
以及如何显示要检查的错误代码
<p id="error2">${errors}</p>
我只是想知道是否有更好/更有效的方法来实现这一点。因为这对我正在努力实现的解决方案有效,但我忍不住认为有更好的方法来做到这一点。
暂无答案!
目前还没有任何答案,快来回答吧!