Search in sources :

Example 21 with ObjectError

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ObjectError(org.springframework.validation.ObjectError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 22 with ObjectError

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;
}
Also used : ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) MapBindingResult(org.springframework.validation.MapBindingResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 23 with ObjectError

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;
}
Also used : ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) MapBindingResult(org.springframework.validation.MapBindingResult) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with ObjectError

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: &quot; &#39;".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 &amp;".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: &amp;".equals(nameError.getDefaultMessage()));
    assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode()));
    assertTrue("Name value escaped", "empty &amp;".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: &lt;tag&gt;".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: &lt;tag&gt;".equals(ageError2.getDefaultMessage()));
    assertTrue("Age error 2 code not escaped", "AGE_NOT_32 <tag>".equals(ageError2.getCode()));
}
Also used : Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) TestBean(org.springframework.tests.sample.beans.TestBean) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 25 with ObjectError

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);
        }
    }
}
Also used : ObjectError(org.springframework.validation.ObjectError)

Aggregations

ObjectError (org.springframework.validation.ObjectError)57 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 BindException (org.springframework.validation.BindException)10 FieldError (org.springframework.validation.FieldError)8 BusinessRuleException (org.mifos.service.BusinessRuleException)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 BindingResult (org.springframework.validation.BindingResult)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 Test (org.junit.Test)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)5 MapBindingResult (org.springframework.validation.MapBindingResult)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5 ArrayList (java.util.ArrayList)3 Errors (org.springframework.validation.Errors)3 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)3 ServletException (javax.servlet.ServletException)2