From 0300504fedd22ebacc8a119572b64dd43b0d1d12 Mon Sep 17 00:00:00 2001 From: Parth59 Date: Sun, 6 Jun 2021 20:42:14 -0700 Subject: [PATCH] Added Changes to UserFormController.java to redirect error ot user. --- api/src/main/resources/messages.properties | 2 +- .../web/controller/user/UserFormController.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/api/src/main/resources/messages.properties b/api/src/main/resources/messages.properties index f8c23d3f..3eb054e2 100644 --- a/api/src/main/resources/messages.properties +++ b/api/src/main/resources/messages.properties @@ -6,7 +6,7 @@ legacyui.manageuser.providerAccount=Provider Account legacyui.manageuser.createProviderAccount=Create a Provider account for this user legacyui.manageuser.providerIdentfier=Provider Identifier(s): legacyui.manageuser.noProviderIdentifier=No Identifier Specified - +legacyui.manageuser.DemographicInfo.lengthExceeded=Error in Demographic Info fields. The maximum allowed length of each field is {0}. ${project.parent.artifactId}.Location.purgeLocation=Permanently Delete Location ${project.parent.artifactId}.Location.confirmDelete=Are you sure you want to delete this Location? It will be permanently removed from the system. ${project.parent.artifactId}.Location.purgedSuccessfully=Location deleted successfully diff --git a/omod/src/main/java/org/openmrs/web/controller/user/UserFormController.java b/omod/src/main/java/org/openmrs/web/controller/user/UserFormController.java index f8784094..095b19e7 100644 --- a/omod/src/main/java/org/openmrs/web/controller/user/UserFormController.java +++ b/omod/src/main/java/org/openmrs/web/controller/user/UserFormController.java @@ -33,6 +33,7 @@ import org.springframework.ui.ModelMap; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; @@ -273,7 +274,23 @@ public String handleSubmission(WebRequest request, HttpSession httpSession, Mode userValidator.validate(user, errors); + if (errors.hasErrors()) { + + //check if length exceeded in DemographicInfo fields + List fieldErrorList = errors.getFieldErrors(); + for (FieldError fieldError : fieldErrorList) { + String[] errorCodes = fieldError.getCodes(); + for (String value : errorCodes) { + if (value.contains("error.exceededMaxLengthOfField")) { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"legacyui.manageuser.DemographicInfo.lengthExceeded"); + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ARGS, "50"); + return "redirect:/admin/users/user.form?userId=" + request.getParameter("userId"); + } + } + } + + //check and inform the user for errors response.setStatus(HttpStatus.BAD_REQUEST.value()); return showForm(user.getUserId(), createNewPerson, user, model); }