List of usage examples for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace
public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode)
From source file:org.wise.portal.presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.java
/** * Maybe you want to be provided with the _page parameter (in order to map the same method for all), as you have in * AbstractWizardFormController.//from w w w. j a va 2 s. co m */ @RequestMapping(method = RequestMethod.POST) public String processPage(@RequestParam("_page") final int currentPage, final @ModelAttribute("passwordReminderParameters") PasswordReminderParameters passwordReminderParameters, BindingResult result, SessionStatus status, ModelMap modelMap, final HttpServletResponse response) { switch (currentPage) { case 1: // handle the submit username try { String username = passwordReminderParameters.getUsername(); if (username == null) { result.rejectValue("username", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorNoUsername"); } else { username = StringUtils.trimToNull(username); User user = userService.retrieveUserByUsername(username); /* check to see if user exists and ensure that user is a student */ if (user == null || !(user.getUserDetails() instanceof StudentUserDetails)) { result.rejectValue("username", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorUsernameNotFound"); } } } catch (EmptyResultDataAccessException e) { result.rejectValue("username", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorUsernameNotFound"); } if (result.hasErrors()) { return "forgotaccount/student/passwordreminder"; } // passed validation, put username and account question into page String username = passwordReminderParameters.getUsername(); username = StringUtils.trimToNull(username); User user = userService.retrieveUserByUsername(username); StudentUserDetails userDetails = (StudentUserDetails) user.getUserDetails(); modelMap.put(USERNAME, userDetails.getUsername()); modelMap.put(ACCOUNT_QUESTION, userDetails.getAccountQuestion()); passwordReminderParameters.setAccountQuestion(userDetails.getAccountQuestion()); passwordReminderParameters.setAccountAnswer(userDetails.getAccountAnswer()); return "forgotaccount/student/passwordreminder2"; case 2: // handle the submit with account answer ValidationUtils.rejectIfEmptyOrWhitespace(result, "submittedAccountAnswer", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorSubmittedAccountQuestion"); String submittedAccountAnswer = passwordReminderParameters.getSubmittedAccountAnswer(); String accountAnswer = passwordReminderParameters.getAccountAnswer(); accountAnswer = StringUtils.lowerCase(accountAnswer); submittedAccountAnswer = StringUtils.lowerCase(submittedAccountAnswer); if (accountAnswer == null) { /* * the account answer is null perhaps because the session has * timed out so we will redirect them back to the first * password reminder page where they enter their user name */ return "forgotaccount/student/passwordreminder"; } else if (!accountAnswer.equals(submittedAccountAnswer)) { //they have provided an incorrect account answer result.reject( "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorSubmittedAccountQuestion"); } if (result.hasErrors()) { modelMap.put(USERNAME, passwordReminderParameters.getUsername()); modelMap.put(ACCOUNT_QUESTION, passwordReminderParameters.getAccountQuestion()); return "forgotaccount/student/passwordreminder2"; } // passed validation, go to next page return "forgotaccount/student/passwordreminder3"; case 3: // handle the submit with new passwords ValidationUtils.rejectIfEmptyOrWhitespace(result, "verifyPassword", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorVerifyNewPassword"); ValidationUtils.rejectIfEmptyOrWhitespace(result, "newPassword", "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorVerifyNewPassword"); if (result.hasErrors()) { return "forgotaccount/student/passwordreminder3"; } String newPassword = passwordReminderParameters.getNewPassword(); String verifyPassword = passwordReminderParameters.getVerifyPassword(); if (!verifyPassword.equals(newPassword)) { result.reject( "presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.errorVerifyNewPassword"); } if (result.hasErrors()) { return "forgotaccount/student/passwordreminder3"; } // passed validation, save new password String usernameSubmitted = passwordReminderParameters.getUsername(); usernameSubmitted = StringUtils.trimToNull(usernameSubmitted); User userSubmitted = userService.retrieveUserByUsername(usernameSubmitted); if (newPassword != null) { userService.updateUserPassword(userSubmitted, newPassword); } //clear the command object from the session status.setComplete(); modelMap.put("username", passwordReminderParameters.get(PasswordReminderParameters.USERNAME)); return "forgotaccount/student/passwordreminder4"; } return null; }
From source file:uk.ac.ebi.metabolights.validate.ValidatorMetabolightsUser.java
/** * Validation for {@link uk.ac.ebi.metabolights.model.MetabolightsUser MetabolightsUser}, * looking at required fields, email being likely, minimum/maximum field lengths. * /*from w ww . j a va 2 s .co m*/ */ @Override public void validate(Object obj, Errors e) { MetabolightsUser user = (MetabolightsUser) obj; // Required fields // :( not so nice to hard code attribute names here as Strings.. ah, Joy of Spring. ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "NotEmpty.metabolightsUser.firstName"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "NotEmpty.metabolightsUser.lastName"); //ValidationUtils.rejectIfEmptyOrWhitespace(e, "userName", "NotEmpty.metabolightsUser.userName"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "dbPassword", "NotEmpty.metabolightsUser.dbPassword"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "userVerifyDbPassword", "NotEmpty.metabolightsUser.userVerifyDbPassword"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "email", "NotEmpty.metabolightsUser.email"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "affiliation", "NotEmpty.metabolightsUser.affiliation"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "affiliationUrl", "NotEmpty.metabolightsUser.affiliationUrl"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "address", "NotEmpty.metabolightsUser.address"); ValidationUtils.rejectIfEmptyOrWhitespace(e, "apiToken", "NotEmpty.metabolightsUser.apiToken"); // Check email valid pattern (TODO: better use regex) if (e.getFieldError("email") == null) if (!EmailService.isValidEmailAddress(user.getEmail())) e.rejectValue("email", "Email.metabolightsUser.email"); if (e.getFieldError("dbPassword") == null) { // Check two provided passwords match (prevent typo) if (!(user.getDbPassword().equals(user.getUserVerifyDbPassword()))) e.rejectValue("dbPassword", "Match.metabolightsUser.passwords"); // Check minimum size password else if (user.getDbPassword().length() < MIN_PASSWORD_LEN) { e.rejectValue("dbPassword", "Size.metabolightsUser.password"); } } if (!user.getOrcId().equals("")) { String validation = validateORCID(user.getOrcId()); if (validation != null) { e.rejectValue("orcid", null, validation); } } }