Example usage for org.springframework.validation BindingResult getRawFieldValue

List of usage examples for org.springframework.validation BindingResult getRawFieldValue

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult getRawFieldValue.

Prototype

@Nullable
Object getRawFieldValue(String field);

Source Link

Document

Extract the raw field value for the given field.

Usage

From source file:com.br.uepb.validator.adapter.CustomConstraintSpringValidatorAdapter.java

@Override
protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
    for (ConstraintViolation<Object> violation : violations) {
        String field = violation.getPropertyPath().toString();
        FieldError fieldError = errors.getFieldError(field);
        if (fieldError == null || !fieldError.isBindingFailure()) {
            try {
                String errorCode = violation.getConstraintDescriptor().getAnnotation().annotationType()
                        .getSimpleName();
                Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field,
                        violation.getConstraintDescriptor());
                if (errors instanceof BindingResult) {
                    // can do custom FieldError registration with invalid value from ConstraintViolation,
                    // as necessary for Hibernate Validator compatibility (non-indexed set path in field)
                    BindingResult bindingResult = (BindingResult) errors;
                    String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
                    String nestedField = bindingResult.getNestedPath() + field;
                    ObjectError error;//from  w w w  . j a v a  2s  .c o  m
                    if ("".equals(nestedField)) {
                        error = new ObjectError(errors.getObjectName(), errorCodes, errorArgs,
                                violation.getMessage());
                    } else {
                        Object invalidValue = violation.getInvalidValue();
                        if (!"".equals(field) && invalidValue == violation.getLeafBean()) {
                            // bean constraint with property path: retrieve the actual property value
                            invalidValue = bindingResult.getRawFieldValue(field);
                        }
                        if (violation.getMessage() != null && violation.getMessage().startsWith("{")
                                && violation.getMessage().endsWith("}")) {
                            String keyMessage = violation.getMessage();
                            keyMessage = keyMessage.replace("{", "");
                            keyMessage = keyMessage.replace("}", "");
                            List<String> temp = new ArrayList<String>(Arrays.asList(errorCodes));
                            temp.add(keyMessage);
                            errorCodes = temp.toArray(new String[temp.size()]);
                            error = new FieldError(errors.getObjectName(), nestedField, invalidValue, false,
                                    errorCodes, errorArgs, violation.getMessage());
                        } else {
                            error = new FieldError(errors.getObjectName(), nestedField, invalidValue, false,
                                    errorCodes, errorArgs, violation.getMessage());
                        }
                    }
                    bindingResult.addError(error);
                } else {
                    // got no BindingResult - can only do standard rejectValue call
                    // with automatic extraction of the current field value
                    if (violation.getMessage() != null && violation.getMessage().startsWith("{")
                            && violation.getMessage().endsWith("}")) {
                        String keyMessage = violation.getMessage();
                        keyMessage = keyMessage.replace("{", "");
                        keyMessage = keyMessage.replace("}", "");
                        errors.rejectValue(field, keyMessage);
                    } else {
                        errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
                    }
                }
            } catch (NotReadablePropertyException ex) {
                throw new IllegalStateException("JSR-303 validated property '" + field
                        + "' does not have a corresponding accessor for Spring data binding - "
                        + "check your DataBinder's configuration (bean property versus direct field access)",
                        ex);
            }
        }
    }
}

From source file:com.company.simple.util.validator.GlobeValidator.java

protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
    for (ConstraintViolation<Object> violation : violations) {
        String field = violation.getPropertyPath().toString();
        FieldError fieldError = errors.getFieldError(field);
        if (fieldError == null || !fieldError.isBindingFailure()) {
            try {
                ConstraintDescriptor<?> cd = violation.getConstraintDescriptor();
                String errorCode = cd.getAnnotation().annotationType().getSimpleName();
                Object[] errorArgs = getArgumentsForConstraint(errors.getObjectName(), field, cd);
                if (errors instanceof BindingResult) {
                    // Can do custom FieldError registration with invalid value from ConstraintViolation,
                    // as necessary for Hibernate Validator compatibility (non-indexed set path in field)
                    BindingResult bindingResult = (BindingResult) errors;
                    String nestedField = bindingResult.getNestedPath() + field;
                    if ("".equals(nestedField)) {
                        String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
                        bindingResult.addError(new ObjectError(errors.getObjectName(), errorCodes, errorArgs,
                                violation.getMessage()));
                    } else {
                        Object invalidValue = violation.getInvalidValue();
                        if (!"".equals(field) && (invalidValue == violation.getLeafBean()
                                || (field.contains(".") && !field.contains("[]")))) {
                            // Possibly a bean constraint with property path: retrieve the actual property value.
                            // However, explicitly avoid this for "address[]" style paths that we can't handle.
                            invalidValue = bindingResult.getRawFieldValue(field);
                        }//from  w w  w  . j av  a 2  s  . c  o m
                        String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
                        bindingResult.addError(new FieldError(errors.getObjectName(), nestedField, invalidValue,
                                false, errorCodes, errorArgs, violation.getMessage()));
                    }
                } else {
                    // got no BindingResult - can only do standard rejectValue call
                    // with automatic extraction of the current field value
                    errors.rejectValue(field, errorCode, errorArgs, violation.getMessage());
                }
            } catch (NotReadablePropertyException ex) {
                throw new IllegalStateException("JSR-303 validated property '" + field
                        + "' does not have a corresponding accessor for Spring data binding - "
                        + "check your DataBinder's configuration (bean property versus direct field access)",
                        ex);
            }
        }
    }
}