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.
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.
精彩评论