Sometimes it may require actions which have to get executed without validation or after partial validation. You may find dozen dozens of posts in various forums, blogs regarding skipping JSF validation. But very often we find a satisfactory result. I don’t want to mean we will not find right solution or we do not solve the problem, what I mean we can solve the problem but after lots of tweak, lots of $$ time spending.
Think I have a simple form with some input text components with required attribute true and I am going to refresh this form with new set of values by clicking a command button. How much extra labor I have to pay to solve this? Ok forget this scenario now think I have a form with some input text and dropdown components with required attribute true and I want to populate one dropdown component depends on another and even one dropdown component may not need to validate at all where as another one may need to validate the data based on which it will be populated. Again do you think how much extra labor I have to pay to solve this and even should I do this extra work? Hmm, I Know I have no easy way to work around this problem. Here I will share my thoughts which may help you finding an easy solution to work around the problems that I was discussed above.
First agree with me that we do not make command, dropdown or any other component immediate for skipping validation. Honestly most of the cases immediate attribute does not help you to solve your problem rather it insists you to do extra work.
An easy solution is to do something like below:
<!-- text1 need to be validated only when command button 'frmName:btnSave' will be pressed -->
<h: inputText id=”text1” value=”” required=”#{!empty param[‘frmName:btnSave’]}” />
If you want to execute an action without any validation check like after pressing “cancel”, “refresh”, “clear all” button, you can follow above solution. But to solve more complicated scenario like the second scenario that I discussed at the beginning of this post we will have to do some more work.
First group your input components (whose data need to be validated at validation phase) based on component validation requirement before an action execution and action execution order (think one group of components may need to be validated before executing an action, same group may need not to be validated before executing another action), make group as large as possible so that same component does not belongs to two group and think a name of each group like requireValidationGrp1, requireValidationGrp2 etc. Now use this group as request parameter name inside value expression of required attribute. See below:
<!-- text1 need to be validated only when request parameter 'requireValidationGrp1' has value 1 -->
<h: inputText id=”text1” value=”” required=”#{param[‘ requireValidationGrp1’]==’1’}” />
<!-- text2 need to be validated only when request parameter 'requireValidationGrp2' has value 1 -->
<h: inputText id=”text2” value=”” required=”#{param[‘ requireValidationGrp2’]==’1’}” />
<!-- we do not need to validate text3 at all -->
<h: inputText id=”text3” value=”” required=”false” />
So one or more group of input components will be validated in validation phase based on some request parameters. Now who push these parameters, when and why? Obviously we will push these parameter with value 1 using JSF tags inside components which has action and which will be executed on validated group of components data. That is how we can control which group of components will be validated in validation phase. See below:
<!--don’t require any validation and hence we don't need to push any request parameter. you can apply this for buttons like cancel, refresh, clear all etc. -->
<h:commandButton value="Clear All"></h:commandButton>
<!-- require validation of two groups: requireValidationGrp1 and requireValidationGrp2 after pressing save button -->
<h:commandButton value="Save">
<f:param name="requireValidationGrp1" value="1"></f:param>
<f:param name="requireValidationGrp2" value="1"></f:param>
</h:commandButton>
<!-- require validation of only one group: requireValidationGrp1 after pressing copy button -->
<h:commandButton value="Copy">
<f:param name="requireValidationGrp1" value="1"></f:param>
</h:commandButton>
See below for a complete example:
<h:panelGroup id="region1" layout="block" style="width: 100%;height: 100%">
<a4j:form id="frm1">
<rich:panel id="panel1" style="width:80%;">
<f:facet name="header">Skipping validation when required</f:facet>
<h:panelGrid columns="1">
<h:panelGrid columns="3" id="grid1">
<h:outputLabel for="input1" value="Input1: "></h:outputLabel>
<h:inputText id="input1" required="#{param['requireValidationGrp2'] == '1'}" requiredMessage="input1 required"></h:inputText>
<rich:message for="input1" />
<h:outputLabel for="input2" value="Input2: "></h:outputLabel>
<h:inputText id="input2" required="#{param['requireValidationGrp1'] == '1'}" requiredMessage="Input2 required"></h:inputText>
<rich:message for="input2" />
<h:outputLabel for="input3" value="input3"></h:outputLabel>
<h:selectOneMenu id="input3" value="0">
<f:selectItem itemLabel="Select" itemValue="0"/>
<f:selectItem itemLabel="s1" itemValue="1"/>
<f:selectItem itemLabel="s2" itemValue="2"/>
<f:selectItem itemLabel="s3" itemValue="3"/>
<f:selectItem itemLabel="s4" itemValue="4"/>
<a4j:support event="onchange" reRender="region1">
<a4j:actionparam name="requireValidationGrp1" value="1">
</a4j:actionparam>
</a4j:support>
</h:selectOneMenu>
<rich:message for="input3" />
</h:panelGrid>
<h:panelGroup>
<h:panelGroup id="btn1" style="float:left;" layout="block">
<a4j:commandButton value="Save" reRender="region1">
<a4j:actionparam name="requireValidationGrp1" value="1">
</a4j:actionparam>
<a4j:actionparam name="requireValidationGrp2" value="1">
</a4j:actionparam>
</a4j:commandButton>
</h:panelGroup>
<h:panelGroup id="btn3" style="float:left" layout="block">
<a4j:commandButton type="button" value="Clear All" reRender="region1">
</a4j:commandButton>
</h:panelGroup>
</h:panelGroup>
</h:panelGrid>
</rich:panel>
</a4j:form>
</h:panelGroup>

No comments yet
Comments feed for this article