Search in sources :

Example 1 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project spring-boot by spring-projects.

the class SpringValidatorTests method wrapLocalValidatorFactoryBean.

@Test
public void wrapLocalValidatorFactoryBean() {
    SpringValidator wrapper = load(LocalValidatorFactoryBeanConfig.class);
    assertThat(wrapper.supports(SampleData.class)).isTrue();
    MapBindingResult errors = new MapBindingResult(new HashMap<String, Object>(), "test");
    wrapper.validate(new SampleData(40), errors);
    assertThat(errors.getErrorCount()).isEqualTo(1);
}
Also used : MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Example 2 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project ORCID-Source by ORCID.

the class ManageProfileController method postEmailsJson.

@Deprecated
@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.toV3Emails());
    }
    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 3 with MapBindingResult

use of org.springframework.validation.MapBindingResult 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()) {
        if (oe.getCode() != null) {
            errors.add(getMessage(oe.getCode(), oe.getArguments()));
        } else {
            errors.add(oe.getDefaultMessage());
        }
    }
    if (errors.isEmpty()) {
        // clear errors
        email.setErrors(new ArrayList<String>());
        String currentUserOrcid = getCurrentUserOrcid();
        emailManager.addEmail(request, currentUserOrcid, email.toV3Email());
    } 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 4 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project entando-core by entando.

the class UserValidator method createDeleteAdminError.

public static BindingResult createDeleteAdminError() {
    Map<String, String> map = new HashMap<>();
    map.put("username", "admin");
    BindingResult bindingResult = new MapBindingResult(map, "username");
    bindingResult.reject(UserValidator.ERRCODE_DELETE_ADMIN, new String[] {}, "user.admin.cant.delete");
    return bindingResult;
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) HashMap(java.util.HashMap) MapBindingResult(org.springframework.validation.MapBindingResult)

Example 5 with MapBindingResult

use of org.springframework.validation.MapBindingResult in project entando-core by entando.

the class GuiFragmentValidatorTest method validateRightFragment.

@Test
public void validateRightFragment() throws Exception {
    when(this.guiFragmentManager.getGuiFragment("not_existing")).thenReturn(null);
    GuiFragmentRequestBody request = new GuiFragmentRequestBody("not_existing", "<h1>code</h1>");
    MapBindingResult bindingResult = new MapBindingResult(new HashMap<Object, Object>(), "fragment");
    validator.validate(request, bindingResult);
    Assert.assertFalse(bindingResult.hasErrors());
    Assert.assertEquals(0, bindingResult.getErrorCount());
}
Also used : GuiFragmentRequestBody(org.entando.entando.web.guifragment.model.GuiFragmentRequestBody) MapBindingResult(org.springframework.validation.MapBindingResult) Test(org.junit.Test)

Aggregations

MapBindingResult (org.springframework.validation.MapBindingResult)40 Test (org.junit.Test)22 ObjectError (org.springframework.validation.ObjectError)12 BindingResult (org.springframework.validation.BindingResult)11 HashMap (java.util.HashMap)10 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)9 Test (org.junit.jupiter.api.Test)6 Relationship (org.openmrs.Relationship)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 Method (java.lang.reflect.Method)5 GuiFragmentRequestBody (org.entando.entando.web.guifragment.model.GuiFragmentRequestBody)5 MethodParameter (org.springframework.core.MethodParameter)5 BindException (org.springframework.validation.BindException)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ServletException (jakarta.servlet.ServletException)3 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)3 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 FieldError (org.springframework.validation.FieldError)3 WebExchangeBindException (org.springframework.web.bind.support.WebExchangeBindException)3