Example usage for org.springframework.validation FieldError getCode

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

Introduction

In this page you can find the example usage for org.springframework.validation FieldError 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:se.skltp.cooperation.web.rest.exception.DefaultExceptionHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//from  w  ww. j  av  a2  s  .c o m
@ResponseBody
public ProblemDetail processValidationError(HttpServletRequest request, MethodArgumentNotValidException e) {
    log.debug("Handling form validation error");

    ValidationError error = new ValidationError();
    buildErrorMessage(request, e, HttpStatus.BAD_REQUEST, error);

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    for (FieldError fieldError : fieldErrors) {
        log.debug("Adding error message: {} to field: {}", fieldError.getDefaultMessage(),
                fieldError.getField());
        error.addFieldError(fieldError.getField(), fieldError.getCode(), fieldError.getDefaultMessage());
    }

    return error;
}

From source file:com.springsource.greenhouse.signup.SignupController.java

private List<Map<String, String>> getErrorsMap(BindingResult formBinding) {
    List<FieldError> fieldErrors = formBinding.getFieldErrors();
    List<Map<String, String>> errors = new ArrayList<Map<String, String>>(fieldErrors.size());
    for (FieldError fieldError : fieldErrors) {
        Map<String, String> fieldErrorMap = new HashMap<String, String>();
        fieldErrorMap.put("field", fieldError.getField());
        fieldErrorMap.put("code", fieldError.getCode());
        fieldErrorMap.put("message", fieldError.getDefaultMessage());
        errors.add(fieldErrorMap);// w  w  w . j  ava 2 s.  c  om
    }
    return errors;
}

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

public void testValidateIndentationFormat() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("bad");

    mVal.validate(u, errors);/*w  w w  .  ja  va  2  s .  c  om*/

    assertEquals("indentationFormat", 1, errors.getFieldErrorCount("indentationFormat"));
    FieldError err = errors.getFieldError("indentationFormat");
    assertEquals("indentation format", "error.indentation.format", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setIndentationFormat("nbsp");
    mVal.validate(u, errors);

    assertEquals("indentationFormat good", 0, errors.getFieldErrorCount("indentationFormat"));
}

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

public void testValidateHierarchyPosition() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("nbsp");
    u.setHierarchyPosition("bad");

    mVal.validate(u, errors);//from  w ww .ja v a2 s  .  c o  m

    assertEquals("hierarchyPosition", 1, errors.getFieldErrorCount("hierarchyPosition"));
    FieldError err = errors.getFieldError("hierarchyPosition");
    assertEquals("hierarchy position", "error.hierarchy.position", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setHierarchyPosition("above");
    mVal.validate(u, errors);

    assertEquals("hierarchyPosition good", 0, errors.getFieldErrorCount("hierarchyPosition"));
}

From source file:com.iflytek.edu.cloud.frame.spring.DefaultHandlerExceptionResolver.java

/**
 * ??/*from w ww. j a va  2  s  .  c o m*/
 *
 * @param allErrors
 * @param locale
 * @param subErrorType
 * @return
 */
private MainError getBusinessParameterMainError(List<ObjectError> allErrors, Locale locale,
        SubErrorType subErrorType) {
    MainError mainError = SubErrors.getMainError(subErrorType, locale);
    for (ObjectError objectError : allErrors) {
        if (objectError instanceof FieldError) {
            FieldError fieldError = (FieldError) objectError;
            SubErrorType tempSubErrorType = INVALIDE_CONSTRAINT_SUBERROR_MAPPINGS.get(fieldError.getCode());
            if (tempSubErrorType == subErrorType) {
                SubError subError = SubErrors.getSubError(tempSubErrorType.value(), locale,
                        fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage());
                mainError.addSubError(subError);
            }
        }
    }
    return mainError;
}

From source file:mvc.PaymentController.java

private String getErrors(BindingResult result, HttpServletRequest req) {
    String errorString = "";
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(req.getServletContext());
    ResourceBundleMessageSource rbms = applicationContext.getBean(ResourceBundleMessageSource.class);
    for (FieldError error : result.getFieldErrors()) {
        errorString += rbms.getMessage(error.getCode(), null, Locale.US);
    }/*ww w. j a v a2s .  co  m*/
    return errorString;
}

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

public void testValidateForumControlLocation() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("nbsp");
    u.setHierarchyPosition("above");
    u.setLinkFormat("split");
    u.setImageFormat("inline");
    u.setForumControlLocation("bad");

    mVal.validate(u, errors);//  w  w  w . j  a  v  a  2  s.co  m

    assertEquals("forumControlLocation", 1, errors.getFieldErrorCount("forumControlLocation"));
    FieldError err = errors.getFieldError("forumControlLocation");
    assertEquals("forumControlLocation code", "error.forum.control.location", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setForumControlLocation("top");
    mVal.validate(u, errors);

    assertEquals("forumControlLocation good", 0, errors.getFieldErrorCount("forumControlLocation"));
}

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

public void testValidateForumBatchSize() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("nbsp");
    u.setHierarchyPosition("above");
    u.setLinkFormat("split");
    u.setImageFormat("inline");
    u.setForumControlLocation("inline");
    u.setForumBatchSize(0);/*  w  w w . j  av a 2  s  .c o m*/

    mVal.validate(u, errors);

    assertEquals("forumBatchSize min", 1, errors.getFieldErrorCount("forumBatchSize"));
    FieldError err = errors.getFieldError("forumBatchSize");
    assertEquals("forum batch size min type", "error.forum.batch.size.min", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setForumBatchSize(2000);
    mVal.validate(u, errors);

    assertEquals("forumBatchSize max", 1, errors.getFieldErrorCount("forumBatchSize"));
    err = errors.getFieldError("forumBatchSize");
    assertEquals("forum batch size max type", "error.forum.batch.size.max", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setForumBatchSize(50);
    mVal.validate(u, errors);

    assertEquals("forumBatchSize good", 0, errors.getFieldErrorCount("forumBatchSize"));
}

From source file:org.frat.common.exception.BindingResultExceptionHandler.java

/**
 * /*  www .j  a v  a2s  .c om*/
 * Description: set the result dto.
 * 
 * @param locale
 * 
 * @param constraintViolation
 * @throws NoSuchFieldException
 */
private void setResultDto(BindingResult bindingResult, List<ValidationResultDto> errorData, String formId,
        boolean notManually) {
    List<FieldError> fieldErros = bindingResult.getFieldErrors();
    String beanName = bindingResult.getObjectName();
    Object rootObject = bindingResult.getTarget();
    Class<?> rootClass = rootObject.getClass();
    if (fieldErros != null && fieldErros.size() > 0) {
        for (FieldError fieldError : fieldErros) {
            final String fieldName = fieldError.getField();
            String message = fieldError.getDefaultMessage();
            final String errorCode = fieldError.getCode();
            if (StringUtils.isEmpty(message)) {
                message = MessageUtil.getMessage(StringUtils.isNotEmpty(errorCode) ? errorCode : message);
            }
            setFieldErrorMap(fieldName, beanName, rootClass, errorData, message, formId, notManually);
        }
    }
}

From source file:org.apigw.authserver.web.admin.DefaultAdminExceptionHandler.java

private ValidationErrorDTO processFieldErrors(List<FieldError> fieldErrors) {
    ValidationErrorDTO dto = new ValidationErrorDTO();

    for (FieldError fieldError : fieldErrors) {

        String localizedErrorMessage = null; //resolveLocalizedErrorMessage(fieldError);
        log.debug("Adding error message: {} to field: {}", localizedErrorMessage, fieldError.getField());
        dto.addFieldError(fieldError.getField(), fieldError.getCode(), localizedErrorMessage);
    }//  w  ww .j a  va2s .co m

    return dto;
}