Example usage for org.springframework.validation ObjectError getCode

List of usage examples for org.springframework.validation ObjectError getCode

Introduction

In this page you can find the example usage for org.springframework.validation ObjectError getCode.

Prototype

@Nullable
public String getCode() 

Source Link

Document

Return the default code of this resolvable, that is, the last one in the codes array.

Usage

From source file:com.kixeye.chassis.transport.ExceptionServiceErrorMapper.java

/**
 * Maps an exception to an error.//from   ww w  . j  a v  a 2  s .co  m
 * 
 * @param ex
 * @return
 */
public static ServiceError mapException(Throwable ex) {
    ServiceError error = null;

    if (ex instanceof ServiceException) {
        ServiceException servEx = (ServiceException) ex;

        error = servEx.error;
    } else if (ex instanceof MethodArgumentNotValidException) {
        MethodArgumentNotValidException validationEx = (MethodArgumentNotValidException) ex;

        List<String> errors = Lists.newArrayList();

        for (ObjectError objError : validationEx.getBindingResult().getAllErrors()) {
            errors.add(
                    objError.getObjectName() + ":" + objError.getCode() + ":" + objError.getDefaultMessage());
        }

        error = new ServiceError(VALIDATION_ERROR_CODE, Joiner.on("|").join(errors));
    } else {
        error = new ServiceError(UNKNOWN_ERROR_CODE, ex.getMessage());
    }

    return error;
}

From source file:org.openmrs.module.webservices.rest.web.RestUtil.java

/**
 * Creates a SimpleObject to sent to the client with all validation errors (with message codes
 * resolved)//from w  w w  . ja  v  a  2  s  . com
 * 
 * @param ex
 * @return
 */
public static SimpleObject wrapValidationErrorResponse(ValidationException ex) {

    MessageSourceService messageSourceService = Context.getMessageSourceService();

    SimpleObject errors = new SimpleObject();
    errors.add("message", messageSourceService.getMessage("webservices.rest.error.invalid.submission"));
    errors.add("code", "webservices.rest.error.invalid.submission");

    List<SimpleObject> globalErrors = new ArrayList<SimpleObject>();
    SimpleObject fieldErrors = new SimpleObject();

    if (ex.getErrors().hasGlobalErrors()) {

        for (Object errObj : ex.getErrors().getGlobalErrors()) {

            ObjectError err = (ObjectError) errObj;
            String message = messageSourceService.getMessage(err.getCode());

            SimpleObject globalError = new SimpleObject();
            globalError.put("code", err.getCode());
            globalError.put("message", message);
            globalErrors.add(globalError);
        }

    }

    if (ex.getErrors().hasFieldErrors()) {

        for (Object errObj : ex.getErrors().getFieldErrors()) {
            FieldError err = (FieldError) errObj;
            String message = messageSourceService.getMessage(err.getCode());

            SimpleObject fieldError = new SimpleObject();
            fieldError.put("code", err.getCode());
            fieldError.put("message", message);

            if (!fieldErrors.containsKey(err.getField())) {
                fieldErrors.put(err.getField(), new ArrayList<SimpleObject>());
            }

            ((List<SimpleObject>) fieldErrors.get(err.getField())).add(fieldError);
        }

    }

    errors.put("globalErrors", globalErrors);
    errors.put("fieldErrors", fieldErrors);

    return new SimpleObject().add("error", errors);
}

From source file:com.acc.storefront.controllers.misc.AddToCartController.java

protected boolean isTypeMismatchError(final ObjectError error) {
    return error.getCode().equals(TYPE_MISMATCH_ERROR_CODE);
}

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetAllErrorsFunction.java

@Override
public Object execute(Map<String, Object> args) {
    List<String> results = new ArrayList<>();
    String formName = (String) args.get(PARAM_FORM_NAME);

    EvaluationContext context = (EvaluationContext) args.get("_context");
    Locale locale = context.getLocale();
    BindingResult bindingResult = this.getBindingResult(formName, context);

    if (bindingResult != null) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
                    error.getDefaultMessage(), locale);
            if (msg != null) {
                results.add(msg);//  w  w w .jav a  2 s.  c  om
            }
        }
    }
    return results;
}

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetGlobalErrorsFunction.java

@Override
public Object execute(Map<String, Object> args) {
    List<String> results = new ArrayList<>();
    String formName = (String) args.get(PARAM_FORM_NAME);

    EvaluationContext context = (EvaluationContext) args.get("_context");
    Locale locale = context.getLocale();
    BindingResult bindingResult = this.getBindingResult(formName, context);

    if (bindingResult != null) {
        for (ObjectError error : bindingResult.getGlobalErrors()) {
            String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
                    error.getDefaultMessage(), locale);
            if (msg != null) {
                results.add(msg);//from  www .ja va 2  s  .c o  m
            }
        }
    }
    return results;
}

From source file:org.ualerts.fixed.web.controller.AbstractFormController.java

private List<String> getGlobalErrors(Errors errors) {
    if (!errors.hasGlobalErrors())
        return null;

    List<String> globalErrors = new ArrayList<String>();
    for (ObjectError error : errors.getGlobalErrors()) {
        globalErrors.add(getMessage(error.getCode()));
    }//from  w ww.  j ava2  s. com
    return globalErrors;
}

From source file:mx.com.quadrum.contratos.controller.crud.ContactoController.java

@ResponseBody
@RequestMapping(value = "agregarContacto", method = RequestMethod.POST)
public String agregarContacto(@Valid @ModelAttribute("contacto") Contacto contacto, BindingResult bindingResult,
        HttpSession session) {//w w w  .  j a  v a2 s  .  c o  m
    if (session.getAttribute("usuario") == null) {
        return SESION_CADUCA;
    }
    if (bindingResult.hasErrors()) {
        for (ObjectError e : bindingResult.getAllErrors()) {
            System.out.println(e.getCode());
            System.out.println(e.getDefaultMessage());
            System.out.println(e.getObjectName());
            System.out.println(e.toString());
        }
        return ERROR_DATOS;
    }
    if (contactoService.existeCorreo(contacto.getMail())) {
        return "Error...#Ya existe un usuario con el correo que quiere ingresar.";
    }
    return contactoService.agregar(contacto);
}

From source file:org.iwethey.forums.web.user.test.NewUserValidatorTest.java

public void testValidateExists() {
    User u = new User();
    BindException errors = new BindException(u, "userInfo");
    u.setNickname("ut_spork1");
    u.setUnencryptedPassword("itchy1");
    u.setPasswordCheck("itchy1");

    mVal.validate(u, errors);//from   w ww  .  j a  va  2 s .  c om
    assertEquals("nickname", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("password", 0, errors.getFieldErrorCount("password"));
    assertEquals("passwordCheck", 0, errors.getFieldErrorCount("passwordCheck"));

    ObjectError err = errors.getGlobalError();
    assertEquals("global", "error.existing.login", err.getCode());
}

From source file:com.jnj.b2b.storefront.controllers.misc.AddToCartController.java

protected String getViewWithBindingErrorMessages(final Model model, final BindingResult bindingErrors) {
    for (final ObjectError error : bindingErrors.getAllErrors()) {
        if (error.getCode().equals(TYPE_MISMATCH_ERROR_CODE)) {
            model.addAttribute(ERROR_MSG_TYPE, QUANTITY_INVALID_BINDING_MESSAGE_KEY);
        } else {//w  ww.  ja v  a  2 s .c om
            model.addAttribute(ERROR_MSG_TYPE, error.getDefaultMessage());
        }
    }
    return ControllerConstants.Views.Fragments.Cart.AddToCartPopup;
}

From source file:org.openmrs.module.kenyaemr.fragment.controller.developer.DeveloperUtilsFragmentController.java

/**
 * Helper method to extract unique error messages from a bind exception and format them
 * @param errors the bind exception//from   w w  w.j  a  v a 2  s  . co  m
 * @return the messages
 */
protected Set<String> uniqueErrorMessages(BindException errors) {
    Set<String> messages = new LinkedHashSet<String>();
    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;
        }

        messages.add(message);
    }

    return messages;
}