I am using Spring + Hibernate + Spring-MVC .
I want to define a user constraint combining two other predefined validation annotations: @NotNull @Size
as follows:
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@NotNull
@Size(min=4)
public @interface JPasswordConstraint {
}
and I want to use this annotation in my form models.
public class ChangePasswordForm {
@NotNull
private String currentPass;
@JPasswordConstraint
private String newPass;
@JPasswordConstraint
private String newPassConfirm;
}
UserController.java
@RequestMapping(value = "/pass", method = RequestMethod.POST)
public String pass2(Model model, @Valid @ModelAttribute("changePasswordForm") ChangePasswordForm form, BindingResult result) {
model.addAttribute("changePasswordForm", form);
try {
userService.changePassword(form);
} catch (Exception ex) {
result.rejectValue(null, "error.objec", ex.getMessage());
System.out.println(result);
}
if (!result.hasErrors()) {
model.addAttribute("successMessage", "password changed successfully!");
}
return "user/pass";
}
But that will not work. It accepts passwords less than 4 characters.
How can I solve this problem?
source
share