0

I'm trying to create form for editing entity object when selected from datatable. So when user clicks commandButton in datatable, myBean.person property is filled with appropriate person object. Person has "status" property.

I'd like to validate edit form with different validation groups according to value of "status" property. Is this possible?

I created two different validation groups:

 @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;
    }

I'd like to validate form before saving and when status is "new", then name property should be set. When status is "active", then code property should be set.

I have jsf page:

<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>

I tried to use <f:validateBean /> tag with dynamicaly set validationGroups attribute, but method that returned validationGroups was called before actual person object was retrieved. So I couldn't decide according to Person.status property.

So is it possible to define PersonNew as validation group if person has status "new", otherwise define PersonActive as validation group?

Thanks for any help.

4

1 回答 1

0

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

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

官方手册

于 2011-10-05T16:59:21.473 回答