Source:
public void validate(Object target, Errors errors) { RegistrationUser registrationUser = (RegistrationUser) target; boolean isUserNameEmpty = Strings.isNullOrEmpty(registrationUser.getUserName()); if (!registrationService.getUsernameEqualsEmail() && isUserNameEmpty) { errors.rejectValue("userName", "registration.exception.usernameEqualsEmail", "Username not set."); } if (!Strings.isNullOrEmpty(registrationUser.getPhoto())) { try { new URI(registrationUser.getPhoto()); } catch (URISyntaxException e) { errors.rejectValue("photo", "registration.exception.photo", "Photo is not an URI"); } } if (registrationUser.getPassword().length() < registrationService.getPasswordLength()) { String[] argument = { String.valueOf(registrationService.getPasswordLength()) }; errors.rejectValue("password", "registration.exception.password.length", argument, "Your password is not long enough."); } if (!Strings.isNullOrEmpty(registrationUser.getConfirmPassword()) && !registrationUser.getConfirmPassword().equals(registrationUser.getPassword())) { errors.rejectValue("confirmPassword", "registration.exception.password.equality", "Your passwords don't match."); } if (registrationService.getUsernameEqualsEmail() && registrationService.isUsernameIsAllreadyTaken(registrationUser.getEmail())) { errors.rejectValue("email", "registration.exception.email.alreadytaken", "Your email address is already taken."); } else if (!registrationService.getUsernameEqualsEmail() && registrationService.isUsernameIsAllreadyTaken(registrationUser.getUserName())) { errors.rejectValue("userName", "registration.exception.username.alreadytaken", "Your username is already taken."); } }

Target:
public void validate(Object target, Errors errors) { RegistrationUser registrationUser = (RegistrationUser) target; boolean isUserNameEmpty = Strings.isNullOrEmpty(registrationUser.getUserName()); if (!registrationService.getUsernameEqualsEmail() && isUserNameEmpty) { errors.rejectValue("userName", "registration.exception.usernameEqualsEmail", "Username not set."); } if (!Strings.isNullOrEmpty(registrationUser.getPhoto())) { try { new URI(registrationUser.getPhoto()); } catch (URISyntaxException e) { errors.rejectValue("photo", "registration.exception.photo", "Photo is not an URI"); } } if (registrationUser.getPassword().length() < registrationService.getPasswordLength()) { String[] argument = { String.valueOf(registrationService.getPasswordLength()) }; errors.rejectValue("password", "registration.exception.password.length", argument, "Your password is not long enough. Your password has to be at least " + registrationService.getPasswordLength() + " characters."); } if (!Strings.isNullOrEmpty(registrationUser.getConfirmPassword()) && !registrationUser.getConfirmPassword().equals(registrationUser.getPassword())) { errors.rejectValue("confirmPassword", "registration.exception.password.equality", "Your passwords don't match."); } if (registrationService.getUsernameEqualsEmail() && registrationService.isUsernameIsAllreadyTaken(registrationUser.getEmail())) { errors.rejectValue("email", "registration.exception.email.alreadytaken", "Your email address is already taken."); } else if (!registrationService.getUsernameEqualsEmail() && registrationService.isUsernameIsAllreadyTaken(registrationUser.getUserName())) { errors.rejectValue("userName", "registration.exception.username.alreadytaken", "Your username is already taken."); } }