Bean验证组继承不适用于组序列提供程序 [英] Bean Validation Group inheritance isn't working with Group Sequence Provider
问题描述
我目前正在开发一个应用程序,该应用程序存储的联系信息既包含国外地址也包含国内地址,但是也可以禁用该地址.
I'm currently working on implementing an application that stores contact information which contains both foreign and domestic addresses, but the addresses can also be disabled.
我试图定义三个验证组:用于始终验证信息的超级组和用于不同地址状态的两个子类.
I have attempted to define three validation groups: a super group for the always validated info and two child classes for the different address states.
public interface ContactValidation {}
public interface DomesticValidation extends ContactValidation {}
public interface ForeignValidation extends ContactValidation {}
然后我在联系人类上定义了一个@GroupSequenceProvider.
I then defined an @GroupSequenceProvider on the contact class.
public class ContactGroupSequenceProvider implements DefaultGroupSequenceProvider<Contact> {
@Override
public List<Class<?>> getValidationGroups(Contact contact) {
List<Class<?>> defaultGroupSequence = new ArrayList<Class<?>>();
if(contact.getIsForeign()){
defaultGroupSequence.add(Contact.ForeignValidation.class);
} else {
defaultGroupSequence.add(Contact.DomesticValidation.class);
} //TODO: Add Address Disabled Case
defaultGroupSequence.add(Contact.class); //Get a GroupDefinitionException without this line
return defaultGroupSequence;
}
当我使用任何一个地址验证运行该程序时,标记为groups = Contact.ContactValidation.class的字段都不会被验证,即使它是地址验证的超类.
When I run the program with either address validation, the fields marked with groups=Contact.ContactValidation.class are not being validated even though it is a super class of the address validations.
我需要此应用程序一起进行验证,以便所用的用户不必修复错误,提交并获取新的错误.
I need this application to validate together so the used doesn't have to fix an error, submit and get the new errors.
我也想避免总是经过验证的字段是这样的解决方案.
I also would like to avoid solutions with the always validated fields being like this.
@NotBlank(groups={DomesticValidation.class,ForeignValidation.class ,ContactValidation.class})
private String fullContactName;
任何建议表示赞赏!
推荐答案
如果返回子类之一,我也希望使用ContactValidation
标记的约束得到验证.这似乎是Hibernate Validator中的错误,因此您可以在我们的问题跟踪器中打开故障单吗? ?
If you return one of the sub-classes, I'd also expect the constraints tagged with ContactValidation
to be validated. This seems to be a bug in Hibernate Validator, so could you open a ticket in our issue tracker?
作为一种变通办法,您可以尝试将ContactValidation
显式添加到返回的列表中.
As a work-around you might try to explicitly add ContactValidation
to the list you return.
这篇关于Bean验证组继承不适用于组序列提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!