Disable JSF validator via Ajax

Goal

To disable a JSF validator through an Ajax update call

Description

This recipe uses omnifaces to provide something that is not possible with pure JSF at the moment. As a matter of fact, as explained by BalusC in this post, f:xxx tags are tag handlers (not UI components) which run during view build time, not during render time. So, if they are disabled during building of the view, they will always be disabled until the view is recreated. However, with omnifaces, it is possible to add a validator that “wraps” the corresponding JSF validators to provide the desired goal

How to

The following code snippet shows examples for 2 standard JSF validators: the regex and the length ones (notice that the usual parameters for which of those two validators may be used with the tag validator from omnifaces):

<!DOCTYPE html>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
  ...
  xmlns:o="http://omnifaces.org/ui">
...
  <!-- Validators should be checked only when 
       Submit button is clicked 
  -->
  <p:inputText id="myInput"
    value="#{backingBean.myInput}" required="#{param['confirmCheck'] eq true}">
    <o:validator validatorId="javax.faces.RegularExpression"
      pattern="AQ[0-9]+" disabled="#{param['confirmCheck'] ne true}" />
    <o:validator validatorId="javax.faces.Length" maximum="10"
      minimum="10" disabled="#{param['confirmCheck'] ne true}" />
    <!-- On change, update something else on the form but 
         do not trigger validators -->
    <p:ajax event="change" update="...." />
  </p:inputText>
  ...
  <!-- Clicking here should trigger validation -->
  <p:commandButton value="Submit"
    process="@form" action="#{backingBean.submit}"
    update="@form">
    <f:param name="confirmCheck" value="#{true}" />
  </p:commandButton>
...
</ui:composition>

Explanations

See Omnifaces showcase for more details on its own validators.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s