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.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Mark.
  • 23
  • 7
  • My solution for this problem is to validate bean manually. In "save" method I can decide which validation group to use and create Validator to validate given bean. I can create faces messages from returned constraint violations or actually save the Person. – Mark. Sep 06 '11 at 05:10

1 Answers1

0

If you use Hibernate Validator than looks like @GroupSequenceProvider should satisfy your needs:

The @GroupSequence annotation is a standardized Bean Validation annotation [...] it allows you to statically redefine the default group sequence for a class. Hibernate Validator also offers a custom, non standardized annotation - org.hibernate.validator.group.GroupSequenceProvider - which allows for dynamic redefinition of the default group sequence.

See official manual.

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69