0

Is it possible to process multiple forms with ajax call? Consider next example:

Server side:

@Component
@Scope("session")
public class TestBean implements Serializable {
    private String field11;
    private String field21; 
    private String output;

    public void dummyAction() {
        output = "Output: "
                + "field11=" + field11 + "; "
                + "field21=" + field21 + "; ";
    }

    public String getField11() {
        return field11;
    }
    public void setField11(String field11) {
        this.field11 = field11;
    }
    public String getField21() {
        return field21;
    }
    public void setField21(String field21) {
        this.field21 = field21;
    }
    public String getOutput() {
        return output;
    }
    public void setOutput(String output) {
        this.output = output;
    }
}

Client side:

<h:form id="f_1" name="f_1" >
    <p:panelGrid id="pg_1" columns="1">
        <p:outputLabel value="Form1" />
        <p:outputLabel value="field11:" />
        <p:inputText id="field11" value="#{testBean.field11}" />

        <p:commandLink value="Process f_1 and f_2 from f_1"
            process="@form :f_2" action="#{testBean.dummyAction}"
            ajax="true" update="pg_1 :f_2:pg_2">
        </p:commandLink>
        <p:commandLink value="Process field11 and field21 from f_1"
            process="@this field11 :f_2:field21" action="#{testBean.dummyAction}"
            ajax="true" update="pg_1 :f_2:pg_2">
        </p:commandLink>
    </p:panelGrid>      
</h:form>

<h:form id="f_2" name="f_2" >
    <p:panelGrid id="pg_2" columns="1">
        <p:outputLabel value="Form2" />
        <p:outputLabel value="field21:" />
        <p:inputText id="field21" value="#{testBean.field21}" />
    </p:panelGrid>  
</h:form>

When commandLink is clicked, only fields inside parent form are submitted.

Is it possible to process multiple forms when commandLink is clicked using process attribute?

If not, is there a workaround for this issue (I am thinking of remote command for each form but hoping there is a better and simpler solution)?

Working example of client side code using RemoteCommand which I would like to avoid:

<p:outputPanel autoUpdate="true">
    #{testBean.output}
</p:outputPanel>

<h:form id="f_1" name="f_1" >
    <p:panelGrid id="pg_1" columns="1">
        <p:outputLabel value="Form1" />
        <p:outputLabel value="field11:" />
        <p:inputText id="field11" value="#{testBean.field11}" />

        <p:commandLink value="Process f_1 and f_2 from f_1 using rc to process f_2"
            onclick="rcProcessForm2();"
            process="@form" action="#{testBean.dummyAction}"
            ajax="true" update="pg_1 :f_2:pg_2">
        </p:commandLink>        
    </p:panelGrid>
</h:form>

<h:form id="f_2" name="f_2" >
    <p:panelGrid id="pg_2" columns="1">
        <p:outputLabel value="Form2" />
        <p:outputLabel value="field21:" />
        <p:inputText id="field21" value="#{testBean.field21}" />
    </p:panelGrid>
    <!-- Remote command -->
    <p:remoteCommand name="rcProcessForm2"
        partialSubmit="true" process="@form">
    </p:remoteCommand>
</h:form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marin Relatic
  • 238
  • 2
  • 9
  • I think HTML doesn't support submitting multiple forms at once, see http://stackoverflow.com/questions/25339056/understanding-process-and-update-attributes-of-primefaces ffor better understanding else GOD of JSF(@BalusC) will help – techipank Apr 27 '16 at 16:25
  • 2
    @techipank: in plain html full submission you cannot, but with 'ajax' you can, at least with PrimeFaces by using `partialSubmit` AND use a value for the `process` attribute that refers to multiple forms. In this case `process="f_1 f_2"`. PrimeFaces then gathers all values in each form and submits all these via Ajax. Not sure if it is intentional but it worked in 5.2 and 5.3 (did not check earlier versions or later) – Kukeltje Apr 27 '16 at 18:31
  • @Kukeltje: thanks for the info i never tried that but in this question http://stackoverflow.com/questions/9738871/submit-multiple-forms-with-pcommandbutton after discussion i understood we can not process multiple forms at the same time, @Marin Relatic if solution given by @Kukeltje is not working you can call a javascript and submit the form as well like `document.getElementById('f_2').submit();` if you don't wanted to use `remoteCommand`. – techipank Apr 28 '16 at 04:52
  • @techipank: ajax vs non-ajax... read my comment carefully! – Kukeltje Apr 28 '16 at 05:48
  • @techipank I tried your solution and it's working as you described. Still, don't know will I use it since I am not sure if this is intentional behavior or a bug in Primefaces, can't find any documentation for partialSubmit confirming it should work this way with multiple forms. But if you put your solution in an answer, I will accept it – Marin Relatic Apr 29 '16 at 09:26
  • @Kukeltje: please submit an answer so it can be accepted and have more visibility. I can also confirm that your suggestion works. I bet more people are struggling with the same issue. Cheers! – Bill_BsB Feb 13 '17 at 15:22

0 Answers0