Ugo
Ugo

Reputation: 81

Validating different Validation Groups JSF 2.0

I am working on a form for searching clients in a database. This form is actually separated in two forms: let's say the simple search and the advanced search.

I can choose to do an advanced search by clicking on the corresponding radio button. When I click the radio button, the state of my backing bean changes from the SimpleSearchState to the AdvancedSearchState (State Pattern).

Here is the backing bean for my form:

public class SearchContextBean {

   /**
    * Injected beans
    */
    @ManagedProperty(value = "#{simpleSearchStateBean}")
    private SimpleSearchStateBean simpleSearchStateBean;

    @ManagedProperty(value = "#{advancedSearchStateBean}")
    private AdvancedSearchStateBean advancedSearchStateBean;
    /**
     * State
     */
    private SearchState searchState = simpleSearchStateBean;

    /**
     * Properties, validations.
     */
    @NotEmpty(groups={SimpleSearchValidationGroup.class})
    private String          name;
    @Size(max=18, groups={AdvancedSearchValidationGroup.class})
    private String          firstName;

    ...

    }

So I have a the name property which I want to be mandatory for the simple search. On the other hand I'd like the firstName property to be validated on its size only for the advanced search.

Here is a piece of my .xhtml page:

<h:inputText name="name" id="name"
 value="#{cc.attrs.searchContextBean.name}">
    <f:validateBean
      validationGroups="package.to.my.SimpleSearchValidationGroup" />
</h:inputText>
<h:inputText name="firstName" id="firstName"
 value="#{cc.attrs.searchContextBean.firstName}">
    <f:validateBean
      validationGroups="package.to.my.AdvancedSearchValidationGroup" />
</h:inputText>

I've tried to implement the Hibernate's GroupSequenceProvider without success: The overriden method getValidationGroups always received null instead of my bean object. And then even if I changed the group sequence by returning different lists of validation groups class, it didn't change anything in the validation of my web page.

The problem is, I cannot put a disabled attribute in my f:validateBean which is set according to the state of my SearchContextBean because I cannot rerender the form for some reasons (I need to hide/display the error messages containers according to the EL #{name.valid} which stay at true if an error occurs, even after rerendering).

I was hoping there's a cleaner solution for validating one group or another.

Thanks by advance for any help.

Upvotes: 2

Views: 2704

Answers (1)

Mr.J4mes
Mr.J4mes

Reputation: 9266

I have never used Group Validation. However, to me, the code will be much clearer if I separate the Simple search & Advanced search into 2 different forms like this:

<h:selectBooleanCheckbox id="isSimple" value="#{searchBean.simple}">
   <f:ajax render="searchPanel" />
</h:selectBooleanCheckbox>

<h:panelGroup id="searchPanel">
   <h:form rendered="#{searchBean.simple}" id="simpleSearch>
      <h:inputText id="name" value="#{simpleSearchBean.name}" />
      <h:inputText id="age"  value="#{simpleSearchBean.age}" />

      <h:commandButton id="simpleButton" action="#{simpleSearchBean.search}" value="Search" />
   </h:form>

   <h:form rendered="#{not searchBean.simple}" id="advancedSearch>
      <h:inputText id="firstname" value="#{advancedSearchBean.firstName}" />
      <h:inputText id="age"       value="#{advancedSearchBean.age}" />

      <h:commandButton id="advancedButton" action="#{advancedSearchBean.search}" value="Search" />
   </h:form>
</h:panelGroup>

@ManagedBean
@ViewScoped
public class SearchBean {
   private boolean simple;
}

Then you can execute validation separately. Just my 2 cents! Hope that it can helps you :).

Upvotes: 2

Related Questions