Java Code Examples for org.springframework.validation.FieldError#getCodes()

The following examples show how to use org.springframework.validation.FieldError#getCodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EscapedErrors.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private <T extends ObjectError> T escapeObjectError(@Nullable T source) {
	if (source == null) {
		return null;
	}
	String defaultMessage = source.getDefaultMessage();
	if (defaultMessage != null) {
		defaultMessage = HtmlUtils.htmlEscape(defaultMessage);
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(),
				fieldError.getCodes(), fieldError.getArguments(), defaultMessage);
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(), defaultMessage);
	}
}
 
Example 2
Source File: EscapedErrors.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private <T extends ObjectError> T escapeObjectError(@Nullable T source) {
	if (source == null) {
		return null;
	}
	String defaultMessage = source.getDefaultMessage();
	if (defaultMessage != null) {
		defaultMessage = HtmlUtils.htmlEscape(defaultMessage);
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(),
				fieldError.getCodes(), fieldError.getArguments(), defaultMessage);
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(), defaultMessage);
	}
}
 
Example 3
Source File: EscapedErrors.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
	if (source == null) {
		return null;
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value,
				fieldError.isBindingFailure(), fieldError.getCodes(),
				fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(),
				HtmlUtils.htmlEscape(source.getDefaultMessage()));
	}
}
 
Example 4
Source File: ResponseEntityExceptionHandlerEx.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                                                              HttpHeaders headers,
                                                              HttpStatus status,
                                                              WebRequest request) {
    ValidationErrorDto dto = new ValidationErrorDto();
    for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
        String localizedErrorMessage = fieldError.getDefaultMessage();
        String[] fieldErrorCodes = fieldError.getCodes();
        if (fieldErrorCodes != null) {
            localizedErrorMessage = fieldErrorCodes[0];
        }
        dto.addFieldError(fieldError.getField(), localizedErrorMessage);
    }
    return ResponseEntity.badRequest().body(dto);
}
 
Example 5
Source File: SpringExceptionHandler.java    From market with MIT License 5 votes vote down vote up
private String resolveErrorMessage(FieldError fieldError) {
	Locale currentLocale = LocaleContextHolder.getLocale();
	String localizedErrorMessage = messageSource.getMessage(fieldError, currentLocale);

	// Если подходящего сообщения не найдено - попытаться найти ближайшее по коду ошибки
	if (localizedErrorMessage.equals(fieldError.getDefaultMessage())) {
		String[] fieldErrorCodes = fieldError.getCodes();
		localizedErrorMessage = fieldErrorCodes[0];
	}
	return localizedErrorMessage;
}
 
Example 6
Source File: ParamValidateUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 从bindException中获取到参数错误类型,参数错误
 */
public static ModelAndView getParamErrors(BindException be) {

    // 构造error的映射
    Map<String, String> paramErrors = new HashMap<String, String>();
    Map<String, Object[]> paramArgusErrors = new HashMap<String, Object[]>();
    for (Object error : be.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fe = (FieldError) error;
            String field = fe.getField();

            // 默认的message
            String message = fe.getDefaultMessage();
            try {

                contextReader.getMessage(message, fe.getArguments());

            } catch (NoSuchMessageException e) {

                // 如果有code,则从前往后遍历Code(特殊到一般),修改message为code所对应
                for (int i = fe.getCodes().length - 1; i >= 0; i--) {
                    try {
                        String code = fe.getCodes()[i];
                        String info = contextReader.getMessage(code, fe.getArguments());
                        LOG.debug(code + "\t" + info);
                        message = code;
                    } catch (NoSuchMessageException e2) {
                        LOG.debug("");
                    }
                }
            }

            // 最终的消息
            paramErrors.put(field, message);
            paramArgusErrors.put(field, fe.getArguments());
        }
    }

    return ParamValidateUtils.paramError(paramErrors, paramArgusErrors, ErrorCode.FIELD_ERROR);
}
 
Example 7
Source File: ParamValidateUtils.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 从bindException中获取到参数错误类型,参数错误
 */
public static ModelAndView getParamErrors(BindException be) {

    // 构造error的映射
    Map<String, String> paramErrors = new HashMap<String, String>();
    Map<String, Object[]> paramArgusErrors = new HashMap<String, Object[]>();
    for (Object error : be.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fe = (FieldError) error;
            String field = fe.getField();

            // 默认的message
            String message = fe.getDefaultMessage();
            try {

                contextReader.getMessage(message, fe.getArguments());

            } catch (NoSuchMessageException e) {

                // 如果有code,则从前往后遍历Code(特殊到一般),修改message为code所对应
                for (int i = fe.getCodes().length - 1; i >= 0; i--) {
                    try {
                        String code = fe.getCodes()[i];
                        String info = contextReader.getMessage(code, fe.getArguments());
                        LOG.debug(code + "\t" + info);
                        message = code;
                    } catch (NoSuchMessageException e2) {
                        LOG.debug("");
                    }
                }
            }

            // 最终的消息
            paramErrors.put(field, message);
            paramArgusErrors.put(field, fe.getArguments());
        }
    }

    return ParamValidateUtils.paramError(paramErrors, paramArgusErrors, ErrorCode.FIELD_ERROR);
}
 
Example 8
Source File: OriginTrackedFieldError.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private OriginTrackedFieldError(FieldError fieldError) {
    super(fieldError.getObjectName(), fieldError.getField(),
            fieldError.getRejectedValue(), fieldError.isBindingFailure(),
            fieldError.getCodes(), fieldError.getArguments(),
            fieldError.getDefaultMessage());
}
 
Example 9
Source File: EscapedErrors.java    From spring4-understanding with Apache License 2.0 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
	if (source == null) {
		return null;
	}
	if (source instanceof FieldError) {
		FieldError fieldError = (FieldError) source;
		Object value = fieldError.getRejectedValue();
		if (value instanceof String) {
			value = HtmlUtils.htmlEscape((String) value);
		}
		return (T) new FieldError(
				fieldError.getObjectName(), fieldError.getField(), value,
				fieldError.isBindingFailure(), fieldError.getCodes(),
				fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
	}
	else {
		return (T) new ObjectError(
				source.getObjectName(), source.getCodes(), source.getArguments(),
				HtmlUtils.htmlEscape(source.getDefaultMessage()));
	}
}