Jsf 2 JSF2.0+Bean验证+一种形式的多个验证组

Jsf 2 JSF2.0+Bean验证+一种形式的多个验证组,jsf-2,bean-validation,validationgroup,Jsf 2,Bean Validation,Validationgroup,当从datatable中选择时,我正在尝试创建用于编辑实体对象的表单。所以,当用户单击datatable中的commandButton时,myBean.person属性将填充相应的person对象。个人拥有身份和财产 我想根据status属性的值使用不同的验证组验证编辑表单。这可能吗 我创建了两个不同的验证组: @Entity public class Person{ private String status; ... @NotNull

当从datatable中选择时,我正在尝试创建用于编辑实体对象的表单。所以,当用户单击datatable中的commandButton时,myBean.person属性将填充相应的person对象。个人拥有身份和财产

我想根据status属性的值使用不同的验证组验证编辑表单。这可能吗

我创建了两个不同的验证组:

 @Entity
    public class Person{
        private String status;
        ...
        @NotNull(message = "{person.null.name}", groups = PersonNew.class)
        private String name;
        @NotNull(message = "{person.null.code}", groups = PersonActive.class)
        private String code;
    }
我想在保存前验证表单,当状态为“新建”时,应该设置name属性。当“状态”为“活动”时,应设置“代码”属性

我有jsf页面:

<h:form id="personEdit">
    <h:inputText value="#{myBean.person.name}" />
    <h:inputText value="#{myBean.person.code}" />
    ... other fields for other properties ...
    <h:commandButton value="Save" action="#{myBean.save}" />
</h:form>
我尝试使用带有dynamicaly set validationGroups属性的标记,但在检索实际的person对象之前调用了返回validationGroups的方法。所以我不能根据Person.status属性来决定

所以,若person的状态为new,是否可以将personew定义为验证组,否则将PersonActive定义为验证组


感谢您的帮助。

如果您使用Hibernate Validator,那么@GroupSequenceProvider应该可以满足您的需要:

@GroupSequence注释是一个标准化的Bean验证注释[…],它允许您静态地重新定义类的默认组序列。Hibernate Validator还提供了一个自定义的非标准化注释-org.Hibernate.Validator.group.GroupSequenceProvider-允许动态重新定义默认的组序列


请参阅。

我的解决方案是手动验证bean。在save方法中,我可以决定使用哪个验证组,并创建验证器来验证给定的bean。我可以根据返回的约束冲突创建faces消息,或者实际保存此人。