1

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.

jmrodrigg
  • 600
  • 6
  • 24

3 Answers3

2

If you're using @ViewScoped bean you could do the following changes:

First change your selectOpt1 method to type void:

public void selectOpt1() 
{
    this.list1Val = 1;
    this.list2Val = 1;
    //...
}

Give an id to the component you want to update upon selecting items. I assume List 1 in this case:

<h:selectOneMenu id="list1" value="#{MyClass.list1Val}">
    //...
</h:selectOneMenu>

And in your button, add an f:ajax tag that partially submits only that component for processing:

<h:commandButton value="Select all opt1" action="#{MyClass.selectOpt1}">
    <f:ajax execute="@this" render="list1" />
</h:commandButton>

If you plan to update multiple components, you may add their id separated by space:

<f:ajax execute="@this" render="list1 list2" />

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:

RichFaces

As you mentioned you're using RichFaces you could switch the f:ajax tag to a4j:support:

<h:commandButton value="Select all opt1" action="#{MyClass.selectOpt1}">
    <a4j:support event="onclick" reRender=":list1" ajaxSingle="true" />
</h:commandButton>

Or like this using the a4j:commandButton:

<a4j:commandButton value="Select all opt1" reRender=":list1" ajaxSingle="true" />

For more info on this behavior, please take a look at this.

Regards.

Community
  • 1
  • 1
rbento
  • 9,919
  • 3
  • 61
  • 61
  • hmmm... The bean is session scoped, but i'm working on **JSF 1.2** (for some reasons I've been unable to migrate to JSF 2.0 :(). Can this solution be adapted to my JSF version? Thanks! – jmrodrigg Oct 10 '12 at 13:25
  • Well, this wouldn't work for JSF 1.x. One option would be using a library such as RichFaces as this add ajax functionality. I've used it in the past and it's kind of easy to get started. See my edit. – rbento Oct 10 '12 at 13:32
  • Thanks for the answer. It definetly was my fault, I didn't specify what technology I was using. And yes, I also use RichFaces and I work with `a4j:support`, but I can't solve the problem properly. Thank you for the related topic, I will study it. – jmrodrigg Oct 10 '12 at 13:38
  • @jmrodrigg No problem. Partially updating components in a page is truly ajax territory. See my edit and good luck. Regards. – rbento Oct 10 '12 at 13:39
  • Thanks for your help. I've found solution that works using `a4j:commandButton`. In few minutes I'll write an answer specifying the changes I've included to make it work. Thanks a lot!! – jmrodrigg Oct 10 '12 at 14:26
  • 1
    @jmrodrigg Glad it worked. Did you see my `a4j:commandButton`edit? – rbento Oct 10 '12 at 14:31
2

Just let the required attribute depend on the button pressed.

<h:form id="form">
    <c:set var="saveButtonPressed" value="#{not empty param['form:save']}" />
    ...
    <h:inputText ... required="#{saveButtonPressed}" />
    ...
    <h:commandButton id="save" ... />
</h:form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for this different approach. I didn't know that was possible. Althought `a4j:commandButton` suits better for my purpose, it's nice to know different solutions. Thank you! – jmrodrigg Oct 11 '12 at 08:37
  • @BalusC THX many many times! I've spent hours searching for such a solution! – ProgrammingIsAwsome Jan 28 '16 at 10:51
0

After taking into account Bento's advices, i realitzed the only way to make it work as I wanted was using AJAX a4j.

The changes I've had to introduce in my code are:

  1. Replace h:commandButton "select all" buttons with a4j:commandButton.
  2. Define id tags for my h:selectOneMenu components, so I could reRender the components through a4j:commandButton defined before.

The JSF original page has been modified as follows:

(...)
<table>
    <tr>
        <td>
            <a4j:commandButton value="Select all opt1" action="#{MyClass.selectOpt1}"
                reRender="list1,list2,(...)" ajaxSingle="true" />
            <a4j:commandButton value="Select all opt2" action="#{MyClass.selectOpt2}"
                reRender="list1,list2,(...)" ajaxSingle="true" />
            <a4j:commandButton value="Select all opt3" action="#{MyClass.selectOpt3}"
                reRender="list1,list2,(...)" ajaxSingle="true" />
        </td>
    </tr>
    <tr>
        <td>
            <h:outputText value="List 1:" />
        </td>
        <td>
            <h:selectOneMenu value="#{MyClass.list1Val}" id="list1">
                <f:selectItems value="#{MyClass.listOfOptions}" />
            </h:selectOneMenu>
        </td>
    </tr>
    <tr>
        <td>
            <h:outputText value="List 2:" />
        </td>
        <td>
            <h:selectOneMenu value="#{MyClass.list2Val}" id="list2">
                <f:selectItems value="#{MyClass.listOfOptions}" />
            </h:selectOneMenu>
        </td>
    </tr>
(...)

Thanks a lot Bento for your advices!

jmrodrigg
  • 600
  • 6
  • 24