use of org.openmrs.api.ValidationException in project openmrs-module-pihcore by PIH.
the class ResetPasswordPageController method post.
public String post(@RequestParam(value = "activationKey") String activationKey, @RequestParam(value = "newPassword") String newPassword, @RequestParam(value = "confirmPassword") String confirmPassword, @SpringBean("userService") UserService userService, HttpServletRequest request, PageModel model, UiUtils ui) {
model.addAttribute("activationKey", activationKey);
model.addAttribute("newPassword", newPassword);
model.addAttribute("confirmPassword", confirmPassword);
try {
Context.addProxyPrivilege(GET_USERS);
User user = userService.getUserByActivationKey(activationKey);
if (user == null) {
throw new InvalidActivationKeyException("activation.key.not.correct");
}
Context.setLocale(userService.getDefaultLocaleForUser(user));
if (StringUtils.isBlank(newPassword) || StringUtils.isBlank(confirmPassword)) {
throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.required"));
} else if (!newPassword.equals(confirmPassword)) {
throw new ValidationException(ui.message("emr.account.changePassword.newAndConfirmPassword.DoesNotMatch"));
}
userService.changePasswordUsingActivationKey(activationKey, newPassword);
request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE, ui.message("emr.account.changePassword.success"));
request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_TOAST_MESSAGE, "true");
} catch (Exception e) {
request.getSession().setAttribute(EmrConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, ui.message("emr.account.changePassword.fail", new Object[] { e.getMessage() }, Context.getLocale()));
log.warn("An error occurred while trying to reset password", e);
} finally {
Context.removeProxyPrivilege(GET_USERS);
}
return "redirect:index.htm";
}
use of org.openmrs.api.ValidationException in project openmrs-core by openmrs.
the class ValidateUtil method validate.
/**
* Test the given object against all validators that are registered as compatible with the
* object class
*
* @param obj the object to validate
* @throws ValidationException thrown if a binding exception occurs
* @should throw APIException if errors occur during validation
* @should return immediately if validation is disabled
*/
public static void validate(Object obj) throws ValidationException {
if (disableValidation) {
return;
}
Errors errors = new BindException(obj, "");
Context.getAdministrationService().validate(obj, errors);
if (errors.hasErrors()) {
Set<String> uniqueErrorMessages = new LinkedHashSet<>();
for (Object objerr : errors.getAllErrors()) {
ObjectError error = (ObjectError) objerr;
String message = Context.getMessageSourceService().getMessage(error.getCode());
if (error instanceof FieldError) {
message = ((FieldError) error).getField() + ": " + message;
}
uniqueErrorMessages.add(message);
}
String exceptionMessage = "'" + obj + "' failed to validate with reason: ";
exceptionMessage += StringUtils.join(uniqueErrorMessages, ", ");
throw new ValidationException(exceptionMessage, errors);
}
}
Aggregations