use of org.springframework.validation.ObjectError in project OpenClinica by OpenClinica.
the class StudySubjectEndpoint method mapConfirmation.
private Element mapConfirmation(String confirmation, String studySubjectLabel, Errors errors, String label, List<String> error_messages) throws Exception {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document document = docBuilder.newDocument();
Element responseElement = document.createElementNS(NAMESPACE_URI_V1, "createResponse");
Element resultElement = document.createElementNS(NAMESPACE_URI_V1, "result");
resultElement.setTextContent(confirmation);
responseElement.appendChild(resultElement);
if (studySubjectLabel != null) {
Element labelElement = document.createElementNS(NAMESPACE_URI_V1, label);
labelElement.setTextContent(studySubjectLabel);
responseElement.appendChild(labelElement);
}
if (errors != null) {
for (ObjectError error : errors.getAllErrors()) {
Element errorElement = document.createElementNS(NAMESPACE_URI_V1, "error");
String theMessage = messages.getMessage(error.getCode(), error.getArguments(), locale);
errorElement.setTextContent(theMessage);
responseElement.appendChild(errorElement);
}
}
if (error_messages != null && error_messages.size() > 0) {
StringBuilder output_msg = new StringBuilder();
for (String mes : error_messages) {
output_msg.append(mes);
}
Element msgElement = document.createElementNS(NAMESPACE_URI_V1, "error");
msgElement.setTextContent(output_msg.toString());
responseElement.appendChild(msgElement);
}
return responseElement;
}
use of org.springframework.validation.ObjectError in project ORCID-Source by ORCID.
the class ManageProfileController method postEmailsJson.
@RequestMapping(value = "/emails.json", method = RequestMethod.POST)
@ResponseBody
public org.orcid.pojo.ajaxForm.Emails postEmailsJson(HttpServletRequest request, @RequestBody org.orcid.pojo.ajaxForm.Emails emails) {
org.orcid.pojo.ajaxForm.Email newPrime = null;
List<String> allErrors = new ArrayList<String>();
for (org.orcid.pojo.ajaxForm.Email email : emails.getEmails()) {
MapBindingResult mbr = new MapBindingResult(new HashMap<String, String>(), "Email");
validateEmailAddress(email.getValue(), request, mbr);
List<String> emailErrors = new ArrayList<String>();
for (ObjectError oe : mbr.getAllErrors()) {
String msg = getMessage(oe.getCode(), email.getValue());
emailErrors.add(getMessage(oe.getCode(), email.getValue()));
allErrors.add(msg);
}
email.setErrors(emailErrors);
if (email.isPrimary())
newPrime = email;
}
if (newPrime == null) {
allErrors.add("A Primary Email Must be selected");
}
emails.setErrors(allErrors);
if (allErrors.size() == 0) {
emailManager.updateEmails(request, getCurrentUserOrcid(), emails.toV2Emails());
}
return emails;
}
use of org.springframework.validation.ObjectError in project ORCID-Source by ORCID.
the class ManageProfileController method addEmails.
@RequestMapping(value = "/addEmail.json", method = RequestMethod.POST)
@ResponseBody
public org.orcid.pojo.ajaxForm.Email addEmails(HttpServletRequest request, @RequestBody org.orcid.pojo.AddEmail email) {
List<String> errors = new ArrayList<String>();
ProfileEntity profile = profileEntityCacheManager.retrieve(getCurrentUserOrcid());
// Check password
if (orcidSecurityManager.isPasswordConfirmationRequired() && (email.getPassword() == null || !encryptionManager.hashMatches(email.getPassword(), profile.getEncryptedPassword()))) {
errors.add(getMessage("check_password_modal.incorrect_password"));
}
// if blank
if (PojoUtil.isEmpty(email.getValue())) {
errors.add(getMessage("Email.personalInfoForm.email"));
}
MapBindingResult mbr = new MapBindingResult(new HashMap<String, String>(), "Email");
// make sure there are no dups
validateEmailAddress(email.getValue(), false, false, request, mbr);
for (ObjectError oe : mbr.getAllErrors()) {
errors.add(getMessage(oe.getCode(), email.getValue()));
}
if (errors.isEmpty()) {
// clear errors
email.setErrors(new ArrayList<String>());
String currentUserOrcid = getCurrentUserOrcid();
emailManager.addEmail(request, currentUserOrcid, email.toV2Email());
} else {
email.setErrors(errors);
}
return email;
}
use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.
the class EscapedErrorsTests method testEscapedErrors.
@Test
public void testEscapedErrors() {
TestBean tb = new TestBean();
tb.setName("empty &");
Errors errors = new EscapedErrors(new BindException(tb, "tb"));
errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
assertTrue("Correct errors flag", errors.hasErrors());
assertTrue("Correct number of errors", errors.getErrorCount() == 4);
assertTrue("Correct object name", "tb".equals(errors.getObjectName()));
assertTrue("Correct global errors flag", errors.hasGlobalErrors());
assertTrue("Correct number of global errors", errors.getGlobalErrorCount() == 1);
ObjectError globalError = errors.getGlobalError();
assertTrue("Global error message escaped", "message: " '".equals(globalError.getDefaultMessage()));
assertTrue("Global error code not escaped", "GENERAL_ERROR \" '".equals(globalError.getCode()));
ObjectError globalErrorInList = errors.getGlobalErrors().get(0);
assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInList.getDefaultMessage()));
ObjectError globalErrorInAllList = errors.getAllErrors().get(3);
assertTrue("Same global error in list", globalError.getDefaultMessage().equals(globalErrorInAllList.getDefaultMessage()));
assertTrue("Correct field errors flag", errors.hasFieldErrors());
assertTrue("Correct number of field errors", errors.getFieldErrorCount() == 3);
assertTrue("Correct number of field errors in list", errors.getFieldErrors().size() == 3);
FieldError fieldError = errors.getFieldError();
assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode()));
assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name")));
FieldError fieldErrorInList = errors.getFieldErrors().get(0);
assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage()));
assertTrue("Correct name errors flag", errors.hasFieldErrors("name"));
assertTrue("Correct number of name errors", errors.getFieldErrorCount("name") == 1);
assertTrue("Correct number of name errors in list", errors.getFieldErrors("name").size() == 1);
FieldError nameError = errors.getFieldError("name");
assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage()));
assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode()));
assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name")));
FieldError nameErrorInList = errors.getFieldErrors("name").get(0);
assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage()));
assertTrue("Correct age errors flag", errors.hasFieldErrors("age"));
assertTrue("Correct number of age errors", errors.getFieldErrorCount("age") == 2);
assertTrue("Correct number of age errors in list", errors.getFieldErrors("age").size() == 2);
FieldError ageError = errors.getFieldError("age");
assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage()));
assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode()));
assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age")));
FieldError ageErrorInList = errors.getFieldErrors("age").get(0);
assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage()));
FieldError ageError2 = errors.getFieldErrors("age").get(1);
assertTrue("Age error 2 message escaped", "message: <tag>".equals(ageError2.getDefaultMessage()));
assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode()));
}
use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.
the class BindStatus method initErrorMessages.
/**
* Extract the error messages from the ObjectError list.
*/
private void initErrorMessages() throws NoSuchMessageException {
if (this.errorMessages == null) {
this.errorMessages = new String[this.objectErrors.size()];
for (int i = 0; i < this.objectErrors.size(); i++) {
ObjectError error = this.objectErrors.get(i);
this.errorMessages[i] = this.requestContext.getMessage(error, this.htmlEscape);
}
}
}
Aggregations