Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringValidationAdapter cannot handle disabled default constraint violation [SPR-10345] #14979

Closed
spring-projects-issues opened this issue Mar 1, 2013 · 2 comments
Assignees
Labels
in: web type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Mar 1, 2013

Pavla Nováková opened SPR-10345 and commented

If domain object level validation constraint is specified and some custom validator marks explicitly an object field as invalid (instead of default global object):

context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addNode("fieldToMarkAsInvalid").addConstraintViolation();

SpringValidatorAdapter doesn't check for this option and creates FieldError for marked field but rejected value is not field value but domain object itselves. Think this is a bug. For now I use this fix, but I'm not sure if this covers all possible cases:

Object invalidValue = violation.getInvalidValue();
if (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);
} else if (cd instanceof ConstraintDescriptorImpl) {
    ConstraintDescriptorImpl<?> cdImpl = (ConstraintDescriptorImpl<?>) cd;
    if (cdImpl.getElementType().equals(ElementType.TYPE)) {
        BeanWrapperImpl beanWrapper = new BeanWrapperImpl(invalidValue);
        invalidValue = beanWrapper.getPropertyValue(field);
    }
}

in

protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors)

Affects: 3.2.1

Issue Links:

  • #14876 GenericConversionService.convert() throws IllegalArgumentException after updating to Spring 3.2.1
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 11, 2013

Rossen Stoyanchev commented

Fixed formatting.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Mar 13, 2013

Juergen Hoeller commented

This looks like the same fundamental issue as outlined in #14876, and should have been fixed along with it (in recent 3.2.2 snapshots).

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web type: bug
Projects
None yet
Development

No branches or pull requests

2 participants