Thursday, July 7, 2016

How to skip validation in JSF



Problem:

See following JSF example :



If you click on the “clear” button, the username validation will be fired and stop you proceed clear values, which is doesn’t make sense. Is there any way to bypassing the validation in JSF?


<h:inputText id="username" value="#{helloForm.bean.username}" maxlength="10" size="10">
  <f:validateLength minimum="10"/>
  <f:attribute name="labelValue" value="username"/>
</h:inputText>
<t:messages showDetail="true" showSummary="false" replaceIdWithLabel="true" styleClass="color: red"/>
<h:panelGroup>
  <h:commandButton id="search" value="#{msgs['label.button.search']}" action="#{helloForm.search}"  onclick="openLoadingWindow();" />
  <h:outputText value="#{msg['label.blank']}" />
  <h:commandButton id="clear" value="#{msgs['label.button.reset']}" action="#{helloForm.clear}" onclick="print_off();" />
</h:panelGroup>

Solution:


To skip validation, add a immediate=”true” attribute to the clear button.

<h:inputText id="username" value="#{helloForm.bean.username}" maxlength="10" size="10">
  <f:validateLength minimum="10"/>
  <f:attribute name="labelValue" value="username"/> 
</h:inputText>
<t:messages showDetail="true" showSummary="false" replaceIdWithLabel="true" styleClass="color: red"/>
<h:panelGroup>
  <h:commandButton id="search" value="#{msgs['label.button.search']}" action="#{helloForm.search}"  onclick="openLoadingWindow();" />
  <h:outputText value="#{msg['label.blank']}" />
  <h:commandButton id="clear" value="#{msgs['label.button.reset']}" action="#{helloForm.clear}" immediate="true"</bonclick="print_off();" />
</h:panelGroup>

1 comment: