I have one form with several components, some of them have their own validations and/or are mandatory. Then I also have several selectOneMenu components that have the same three selectItem elements in their lists. I have defined three commandButton in order to select the same values for all the lists (one commandButton per possible value).
The JSF section
<table>
<tr>
<td>
<h:outputText value="ID:" />
</td>
<td>
<h:inputText value="#{MyClass.id}" id="inTxt_id" required="true">
<f:validator validatorId="CheckIDPattern" />
</h:inpuText>
<h:message for="inTxt_id" errorClass="error" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<h:commandButton value="Select all opt1" action="#{MyClass.selectOpt1}" />
<h:commandButton value="Select all opt2" action="#{MyClass.selectOpt2}" />
<h:commandButton value="Select all opt3" action="#{MyClass.selectOpt3}" />
</td>
</tr>
<tr>
<td>
<h:outputText value="List 1:" />
</td>
<td>
<h:selectOneMenu value="#{MyClass.list1Val}">
<f:selectItems value="#{MyClass.listOfOptions}" />
</h:selectOneMenu>
</td>
<tr>
<td>
<h:outputText value="List 2:" />
</td>
<td>
<h:selectOneMenu value="#{MyClass.list2Val}">
<f:selectItems value="#{MyClass.listOfOptions}" />
</h:selectOneMenu>
</td>
</tr>
<tr>
//Many other selectOneMenu components...
</tr>
</table>
<h:commandButton action="#{MyClass.saveLists}" value="Submit" />
MyClass.java
public String selectOpt1(){
this.list1Val = 1;
this.list2Val = 1;
//...
return "";
}
//The same idea for selectOpt2 and selectOpt3.
In this context, if my user presses one of my “select all” buttons without filling the ID inputText, it receives the error message, but I don’t want it because I suppose they haven’t finished filling the form. I just want the validations to be executed when the user presses the “Save Lists” button.
I’ve tried adding immediate="true" to my “select all” buttons, it works but it doesn’t update the view from model and then the user doesn’t see the values have changed… so it really doesn’t work.
What do I want to achieve? I just want the validations to be performed when the last button (submit). So my user could use these “select all” buttons without filling previously the mandatory components.
I hope you understand my context. Feel free to ask for any clarification.
Thanks in advance.
If you’re using
@ViewScopedbean you could do the following changes:First change your
selectOpt1method to typevoid:Give an
idto the component you want to update upon selecting items. I assume List 1 in this case:And in your button, add an
f:ajaxtag that partially submits only that component for processing:If you plan to update multiple components, you may add their id separated by space:
I hope it helps.
Edit
Well, sorry, I didn’t noticed you’re using JSF 1.x. In this case it wouldn’t work.
You could use a library such as RichFaces to add ajax functionality though. Please, check out these topics:
ajax behaviour in jsf 1.2
how do i call a jsf action method (in JSF 1.2) using Ajax?
RichFaces
As you mentioned you’re using RichFaces you could switch the
f:ajaxtag toa4j:support:Or like this using the
a4j:commandButton:For more info on this behavior, please take a look at this.
Regards.