Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-6382

@Autowired in Validator doesn't work with PropertyValidator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Invalid
    • 8.0.0-M6
    • None
    • None

    Description

      I have a generic validator:

      @Component
      public class UniqueFieldValidator implements ConstraintValidator<UniqueFieldValidator.UniqueField, String> {
      
          private AbstractEntityRepository<?,?> repository;
      
          private String fieldName;
      
          @Autowired
          private ApplicationContext applicationContext;
      
          @Target({ElementType.METHOD, ElementType.FIELD})
          @Retention(RetentionPolicy.RUNTIME)
          @Constraint(validatedBy = UniqueFieldValidator.class)
          @Documented
          public @interface UniqueField {
      
              String fieldName();
      
              Class<? extends AbstractEntityRepository<?,?>> repository();
      
              String message() default "field.not.unique";
      
              Class<?>[] groups() default {};
      
              Class<? extends Payload>[] payload() default {};
          }
      
          @Override
          public void initialize(UniqueField uniqueField) {
              this.repository = applicationContext.getBean(uniqueField.repository());
              fieldName = uniqueField.fieldName();
          }
      
          @Override
          public boolean isValid(String value, ConstraintValidatorContext ctx) {
              if(isBlank(value)){
                  return true;
              }
              Optional<?> o = repository.getOneByUniqueField(fieldName, value);
              return isFalse(o.isPresent());
          }
      }
      

      It works perfectly fine with SpringMVC Controllers, but when using Wicket's

      new PropertyValidator<>()

      field "repository" is null

      Can you please take care that fields in Validators gets injected as well?

      Attachments

        Activity

          People

            svenmeier Sven Meier
            eximius Kamil
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: