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

@Autowired in Validator doesn't work with PropertyValidator

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: 8.0.0-M6
    • Fix Version/s: None
    • Labels:
      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

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

              Dates

              • Created:
                Updated:
                Resolved: