gstackoverflow
gstackoverflow

Reputation: 37034

How to force Hibernate Validator to validate fields recursively(cascaded)?

I have the following model:

class EmailWrapper {

    @Email
    private String subscriptionEmail;

    private MyClass myClass;
    //get and set
}


class MyClass {
    @Size(min = 12)
    private String str = "short";
    //get and set
}

and following code:

EmailWrapper emailWrapper = new EmailWrapper();
emailWrapper.setSubscriptionEmail("[email protected]");
emailWrapper.setMyClass(new MyClass());
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<EmailWrapper>> constraintViolations = validator.validate(emailWrapper);
System.out.println(constraintViolations.size());

As You can see that emailWrapper.myClass.str violates the @Size(min = 12)

but I see 0 in console. How to fix this?

Upvotes: 5

Views: 859

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

Replace

private MyClass myClass;

by

@Valid
private MyClass myClass;

Upvotes: 6

Related Questions