public boolean supports(Class clazz) {
return BaseUser.class.isAssignableFrom(clazz);
}
public void validate(Object object, Errors e) {
BaseUser user = (BaseUser) object;
ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName",
"error.required", new Object[]{"First Name"},"First name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName",
"error.required", new Object[]{"Last Name"},"Last name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username",
"error.required", new Object[]{"Username"},"Username is required.");
if (isDuplicateUsername(user)) {
e.reject("error.duplicate-field", new Object[]{user.getCredential().getUsername(), "username"}, "User name already exists.");
}
ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress",
"error.required", new Object[]{"Email Address"},"Email address is required.");
if (!ValidatorUtil.isEmail(user.getEmailAddress())) {
e.rejectValue("emailAddress", "error.invalid-email-address",new Object[]{user.getEmailAddress()},"Email Address is invalid.");
}
if (isDuplicateEmail(user)) {
e.reject("error.duplicate-field", new Object[]{user.getEmailAddress(),"email"}, "Email address already exists.");
}
if (user.getIsNew()) {
ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword",
"error.required", new Object[]{"Password"},"Password is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword",
"error.required", new Object[]{"Confirm Password"},"Confirm password is required.");
}
if (!StringUtil.isEmpty(user.getCredential().getNewPassword()) &&
!StringUtil.isEmpty(user.getCredential().getConfirmPassword()) &&
!user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) {
e.reject("error.password-confirmation-did-not-match", "Password confirmation did not match.");
}
}