List of usage examples for org.springframework.validation BindingResult reject
void reject(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./*w w w . ja va 2s. c o 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:org.wise.portal.presentation.web.controllers.teacher.RegisterTeacherController.java
/** * On submission of the signup form, a user is created and saved to the data * store./* www . j a va2 s. c o m*/ * @param accountForm the model object that contains values for the page to use when rendering the view * @param bindingResult the object used for validation in which errors will be stored * @param request the http request object * @param model the object that contains values to be displayed on the page * @return the path of the view to display */ @RequestMapping(method = RequestMethod.POST) protected String onSubmit(@ModelAttribute("teacherAccountForm") TeacherAccountForm accountForm, BindingResult bindingResult, HttpServletRequest request, Model model) { String view = formView; String domain = ControllerUtil.getBaseUrlString(request); String domainWithPort = domain + ":" + request.getLocalPort(); String referrer = request.getHeader("referer"); //get the context path e.g. /wise String contextPath = request.getContextPath(); String registerUrl = contextPath + "/teacher/registerteacher.html"; String updateAccountInfoUrl = contextPath + "/teacher/management/updatemyaccountinfo.html"; if (referrer.contains(domain + registerUrl) || referrer.contains(domainWithPort + registerUrl) || referrer.contains(domain + updateAccountInfoUrl) || referrer.contains(domainWithPort + updateAccountInfoUrl)) { TeacherUserDetails userDetails = (TeacherUserDetails) accountForm.getUserDetails(); //set the sign up date userDetails.setSignupdate(Calendar.getInstance().getTime()); //validate the form teacherAccountFormValidator.validate(accountForm, bindingResult); if (bindingResult.hasErrors()) { //there were errors populateModel(model); view = formView; } else { //there were no errors if (accountForm.isNewAccount()) { try { userDetails.setDisplayname(userDetails.getFirstname() + " " + userDetails.getLastname()); userDetails.setEmailValid(true); User createdUser = this.userService.createUser(userDetails); // send email to new teacher if email server is configured properly NewAccountEmailService newAccountEmailService = new NewAccountEmailService(createdUser, request.getLocale()); Thread thread = new Thread(newAccountEmailService); thread.start(); } catch (DuplicateUsernameException e) { bindingResult.rejectValue("username", "error.duplicate-username", new Object[] { userDetails.getUsername() }, "Duplicate Username."); populateModel(model); view = formView; } } else { // we're updating an existing teacher's account User user = userService.retrieveUserByUsername(userDetails.getUsername()); TeacherUserDetails teacherUserDetails = (TeacherUserDetails) user.getUserDetails(); teacherUserDetails.setCity(userDetails.getCity()); teacherUserDetails.setCountry(userDetails.getCountry()); teacherUserDetails.setCurriculumsubjects(userDetails.getCurriculumsubjects()); teacherUserDetails.setEmailAddress(userDetails.getEmailAddress()); teacherUserDetails.setSchoollevel(userDetails.getSchoollevel()); teacherUserDetails.setSchoolname(userDetails.getSchoolname()); teacherUserDetails.setState(userDetails.getState()); teacherUserDetails.setDisplayname(userDetails.getDisplayname()); teacherUserDetails.setEmailValid(true); teacherUserDetails.setLanguage(userDetails.getLanguage()); String userLanguage = userDetails.getLanguage(); Locale locale = null; if (userLanguage.contains("_")) { String language = userLanguage.substring(0, userLanguage.indexOf("_")); String country = userLanguage.substring(userLanguage.indexOf("_") + 1); locale = new Locale(language, country); } else { locale = new Locale(userLanguage); } request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); userService.updateUser(user); // update user in session request.getSession().setAttribute(User.CURRENT_USER_SESSION_KEY, user); } model.addAttribute(USERNAME_KEY, userDetails.getUsername()); model.addAttribute(DISPLAYNAME_KEY, userDetails.getDisplayname()); view = successView; } } else { //the request is not coming from a valid domain address so we will not allow it bindingResult.reject("Forbidden"); populateModel(model); view = formView; } return view; }
From source file:org.wise.portal.presentation.web.controllers.teacher.TeacherAccountController.java
/** * On submission of the signup form, a user is created and saved to the data * store.// w w w . j av a2 s.c o m * @param accountForm the model object that contains values for the page to use when rendering the view * @param bindingResult the object used for validation in which errors will be stored * @param request the http request object * @param modelMap the object that contains values to be displayed on the page * @return the path of the view to display */ @RequestMapping(value = { "/teacher/join", "/teacher/management/updatemyaccountinfo.html" }, method = RequestMethod.POST) protected String onSubmit(@ModelAttribute("teacherAccountForm") TeacherAccountForm accountForm, BindingResult bindingResult, HttpServletRequest request, ModelMap modelMap) { String referrer = request.getHeader("referer"); //get the context path e.g. /wise String contextPath = request.getContextPath(); String registerUrl = contextPath + "/teacher/join"; String updateAccountInfoUrl = contextPath + "/teacher/management/updatemyaccountinfo.html"; if (referrer != null && (referrer.contains(registerUrl) || referrer.contains(updateAccountInfoUrl))) { TeacherUserDetails userDetails = (TeacherUserDetails) accountForm.getUserDetails(); //there were no errors if (accountForm.isNewAccount()) { //set the sign up date userDetails.setSignupdate(Calendar.getInstance().getTime()); //validate the form teacherAccountFormValidator.validate(accountForm, bindingResult); if (bindingResult.hasErrors()) { //there were errors populateModelMap(modelMap); return "teacher/join"; } try { userDetails.setDisplayname(userDetails.getFirstname() + " " + userDetails.getLastname()); userDetails.setEmailValid(true); User createdUser = this.userService.createUser(userDetails); // send email to new teacher if email server is configured properly NewAccountEmailService newAccountEmailService = new NewAccountEmailService(createdUser, request.getLocale()); Thread thread = new Thread(newAccountEmailService); thread.start(); modelMap.addAttribute(USERNAME_KEY, userDetails.getUsername()); modelMap.addAttribute(DISPLAYNAME_KEY, userDetails.getDisplayname()); return "teacher/joinsuccess"; } catch (DuplicateUsernameException e) { bindingResult.rejectValue("username", "error.duplicate-username", new Object[] { userDetails.getUsername() }, "Duplicate Username."); populateModelMap(modelMap); return "teacher/join"; } } else { // we're updating an existing teacher's account //validate the form teacherAccountFormValidator.validate(accountForm, bindingResult); if (bindingResult.hasErrors()) { //there were errors populateModelMap(modelMap); return "teacher/management/updatemyaccountinfo"; } User user = userService.retrieveUserByUsername(userDetails.getUsername()); TeacherUserDetails teacherUserDetails = (TeacherUserDetails) user.getUserDetails(); teacherUserDetails.setCity(userDetails.getCity()); teacherUserDetails.setCountry(userDetails.getCountry()); teacherUserDetails.setCurriculumsubjects(userDetails.getCurriculumsubjects()); teacherUserDetails.setEmailAddress(userDetails.getEmailAddress()); teacherUserDetails.setSchoollevel(userDetails.getSchoollevel()); teacherUserDetails.setSchoolname(userDetails.getSchoolname()); teacherUserDetails.setState(userDetails.getState()); teacherUserDetails.setDisplayname(userDetails.getDisplayname()); teacherUserDetails.setEmailValid(true); if ("default".equals(userDetails.getLanguage())) { teacherUserDetails.setLanguage(null); } else { teacherUserDetails.setLanguage(userDetails.getLanguage()); } // set user's language (if specified) Locale locale = null; String userLanguage = teacherUserDetails.getLanguage(); if (userLanguage != null) { if (userLanguage.contains("_")) { String language = userLanguage.substring(0, userLanguage.indexOf("_")); String country = userLanguage.substring(userLanguage.indexOf("_") + 1); locale = new Locale(language, country); } else { locale = new Locale(userLanguage); } } else { // user default browser locale setting if user hasn't specified locale locale = request.getLocale(); } request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale); userService.updateUser(user); // update user in session request.getSession().setAttribute(User.CURRENT_USER_SESSION_KEY, user); return "teacher/management/updatemyaccount"; } } else { //the request is not coming from a valid domain address so we will not allow it bindingResult.reject("Forbidden"); populateModelMap(modelMap); return "teacher/join"; } }