You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Sven Meier (JIRA)" <ji...@apache.org> on 2017/05/30 18:11:04 UTC

[jira] [Commented] (WICKET-6382) @Autowired in Validator doesn't work with PropertyValidator

    [ https://issues.apache.org/jira/browse/WICKET-6382?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16029853#comment-16029853 ] 

Sven Meier commented on WICKET-6382:
------------------------------------

Spring is instantiating your UniqueFieldValidator, so it's not Wicket's responsibility to inject fields.

Please check why #initialize(UniqueField) isn't called and - if you can't solve the problem - provide a quickstart so we can debug the problem.

> @Autowired in Validator doesn't work with PropertyValidator
> -----------------------------------------------------------
>
>                 Key: WICKET-6382
>                 URL: https://issues.apache.org/jira/browse/WICKET-6382
>             Project: Wicket
>          Issue Type: Bug
>          Components: wicket-bean-validation, wicket-spring
>    Affects Versions: 8.0.0-M6
>            Reporter: Kamil
>
> I have a generic validator:
> {code}
> @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());
>     }
> }
> {code}
> It works perfectly fine with SpringMVC Controllers, but when using Wicket's
> {code}new PropertyValidator<>(){code} field "repository" is null
> Can you please take care that fields in Validators gets injected as well?



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)