سؤال

I'm using Bean Validation with Hibernate Validator.

How can I tell Hibernate to only validate a certain value of a different value passed the validator before?

@Digits //eg: DDMMYYYY
String dateString;

//this would throw an exception if @Digits fails. How can I stop validation then?
@Future
Date getDate() {
    //formatter: DDMMYYYY
    return formatter.parse(dateString);
}
هل كانت مفيدة؟

المحلول

You could assign the constraints to different groups and then define a group sequence. If you then request the group sequence to be validated (as part of Validator#validate), constraints are validated in the defined order and validation also stops on the first error. See also http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e2804.

Also, I would not use @Digit on the dateString, but @Pattern. Also you seem to annotate fields and getters. It is really recommended to stick to one approach.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top