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

JSR 303 customer ConstraintValidator seems to result in incorrect error codes [SPR-8989] #13629

Closed
spring-projects-issues opened this issue Jan 4, 2012 · 2 comments
Labels
in: core status: bulk-closed

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 4, 2012

David Melia opened SPR-8989 and commented

Hi,

I have created a JSR 303 Custom ConstraintValidator integrating to Spring but seem to get problems resolving object errors. I am currently experiencing this in a Spring MVC REST application however I have created the below JUnit 4test to try and isolate the problem:

Please see the following test:

 
public class ModelTest {

   @Target({ ElementType.TYPE })
   @Retention(RUNTIME)
   @Constraint(validatedBy = CheckModelValidator.class)
   @Documented
   public @interface CheckModel {
      String message() default "CheckModel.validation";

      Class<?>[] groups() default {};

      Class<? extends Payload>[] payload() default {};
   }
   
   public static class CheckModelValidator implements ConstraintValidator<CheckModel, Model> {

      public void initialize(CheckModel constraintAnnotation) {
      }

      public boolean isValid(Model value, ConstraintValidatorContext context) {
         return false;
      }
   }
   
   @CheckModel // model errors seem to resolve to CheckModel.model.,CheckModel.,CheckModel
   public static class Model {
      public String attribute1;
      @NotEmpty // field errors are fine
      public String attribute2;
   }
   
   @Test
   public void test(){
      LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
      validatorFactoryBean.afterPropertiesSet();
      Model model = new Model();
      BindingResult errors = new BeanPropertyBindingResult(model, "model");
      validatorFactoryBean.validate(model, errors);
      System.out.println(errors);
   }
   
}

where the system.out results in

 
org.springframework.validation.BeanPropertyBindingResult: 2 errors
Error in object 'model': codes [CheckModel.model.,CheckModel.,CheckModel]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [model.,]; arguments []; default message []]; default message [CheckModel.validation]
Field error in object 'model' on field 'attribute2': rejected value [null]; codes [NotEmpty.model.attribute2,NotEmpty.attribute2,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [model.attribute2,attribute2]; arguments []; default message [attribute2]]; default message [may not be empty]

So the 'attribute2' field level validation is fine however the object level '@CheckModel' validation generates the codes CheckModel.model.,CheckModel.,CheckModel which seems incorrect. I would have expected 'CheckModel.validation' to be first?? This seems to be because of the way an ObjectError is created in the SpringValidatorAdapter.

Ultimately I want to extend this validator to do some cross field validation on the object e.g.

   public static class CheckModelValidator implements ConstraintValidator<CheckModel, Model> {

      public void initialize(CheckModel constraintAnnotation) {
      }

      public boolean isValid(Model value, ConstraintValidatorContext context) {
         boolean valid = true;
            if(value.attribute1.equals(value.attribute2)){
                  context.disableDefaultConstraintViolation();
                  context.buildConstraintViolationWithTemplate("CheckModel.somevalidation1").addConstraintViolation();
                  valid = false;
            }
            if(value.attribute1.equals(value.attribute3)){
               context.disableDefaultConstraintViolation();
               context.buildConstraintViolationWithTemplate("CheckModel.somevalidation1").addConstraintViolation();
               valid = false;
         }            
         return valid;
      }
   }

however in the above example I would always get the same message codes back 'CheckModel.model.,CheckModel.,CheckModel' so I cannot distinguish the error.

Much Appreciated.

Dave


Affects: 3.1 GA

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 4, 2012

David Melia commented

Also I seem to have created an empty issue #13628 which can be ignored/deleted - sorry.

@spring-projects-issues spring-projects-issues added type: bug status: waiting-for-triage in: core labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug label Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed and removed status: waiting-for-triage labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 12, 2019

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: bulk-closed
Projects
None yet
Development

No branches or pull requests

2 participants