Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating different Validation Groups JSF 2.0
    text
    copied!<p>I am working on a form for searching clients in a database. This form is actually separated in <strong>two</strong> forms: let's say the <strong>simple search</strong> and the <strong>advanced search</strong>.</p> <p>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 (<a href="http://en.wikipedia.org/wiki/State_pattern" rel="nofollow"><strong><em>State Pattern</em></strong></a>).</p> <p>Here is the backing bean for my form:</p> <pre class="lang-java prettyprint-override"><code>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; ... } </code></pre> <p>So I have a the <strong>name</strong> property which I want to be mandatory for the simple search. On the other hand I'd like the <strong>firstName</strong> property to be validated on its size only for the advanced search.</p> <p>Here is a piece of my .xhtml page:</p> <pre class="lang-xml prettyprint-override"><code>&lt;h:inputText name="name" id="name" value="#{cc.attrs.searchContextBean.name}"&gt; &lt;f:validateBean validationGroups="package.to.my.SimpleSearchValidationGroup" /&gt; &lt;/h:inputText&gt; &lt;h:inputText name="firstName" id="firstName" value="#{cc.attrs.searchContextBean.firstName}"&gt; &lt;f:validateBean validationGroups="package.to.my.AdvancedSearchValidationGroup" /&gt; &lt;/h:inputText&gt; </code></pre> <p>I've tried to implement the <a href="http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html/validator-usingvalidator.html#example-rental-car-group-sequence-provider" rel="nofollow"><strong>Hibernate's GroupSequenceProvider</strong></a> without success: The overriden method <code>getValidationGroups</code> 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.</p> <p>The problem is, I cannot put a <strong>disabled</strong> attribute in my <strong>f:validateBean</strong> 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 <strong>true</strong> if an error occurs, even after rerendering).</p> <p>I was hoping there's a cleaner solution for validating one group or another.</p> <p>Thanks by advance for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload