2

I've tried to learn the JSF 2.0 with bean validation at the class level as the following: -

The utility

@Singleton
public class MyUtility {
    public boolean isValid(final String input) {
       return (input != null) || (!input.trim().equals(""));
    }
}

The constraint annotation

@Retention(RetentionPolicy.RUNTIME)
@Target({
    ElementType.TYPE,
    ElementType.ANNOTATION_TYPE,
    ElementType.FIELD
    })
@Constraint(validatedBy = Validator.class)
@Documented
public @interface Validatable {
    String  message() default "Validation is failure";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

The constraint validator

public class Validator extends ConstraintValidator<Validatable, MyBean> {
    //
    //----> Try to inject the utility, but it cannot as null.
    //
    @Inject
    private MyUtility myUtil;

    public void initialize(ValidatableconstraintAnnotation) {
        //nothing
    }

    public boolean isValid(final MyBean myBean, 
                           final ConstraintValidatorContext constraintContext) {

        if (myBean == null) {
            return true;
        }
        //
        //----> Null pointer exception here.
        //
        return this.myUtil.isValid(myBean.getName());
    }
}

The data bean

@Validatable
public class MyBean {
    private String name;
    //Getter and Setter here
}

The JSF backing bean

@Named
@SessionScoped
public class Page1 {
    //javax.validation.Validator
    @Inject
    private Validator validator;

    @Inject
    private MyBean myBean;

    //Submit method
    public void submit() {
        Set<ConstraintViolation<Object>> violations = 
                         this.validator.validate(this.myBean);
        if (violations.size() > 0) {
            //Handle error here.
        }
    }
}

After running I've faced the exception as java.lang.NullPointerException at the class named "Validator" at the line return this.myUtil.isValid(myBean.getName());. I understand that the CDI does not inject my utility instance. Please correct me If I'm wrong.

I'm not sure if I'm doing something wrong or it is a bean validation limitation. Could you please help to explain further?

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Charlee Chitsuk
  • 8,847
  • 2
  • 56
  • 71

1 Answers1

5

Your right, Hibernate Constraint Validator is not registered as a CDI-Bean by default (and though cannot receive dependencies).

Just put the Seam-Validation module on your classpath, and everything should run fine.

BTW: studying the source-code of the module is an excellent example of the elegance and simplicity of CDI extension. It's doesn't need more than a few dozens lines of code to bridge from CDI to hibernate validations...

Jan Groth
  • 14,039
  • 5
  • 40
  • 55
  • 1
    I'm pretty sure Emmanuel and the rest of the EG are adding CDI support for the next round of Bean Validation. – LightGuard Apr 11 '12 at 16:45
  • Indeed that's a [topic](https://hibernate.onjira.com/browse/bval-238) for BV 1.1. You can learn more in the [early draft](http://beanvalidation.org/1.1/spec/#d0e6698) document. Let us know what you think. – Gunnar Apr 12 '12 at 20:51