treeno
treeno

Reputation: 2600

CommandButton outside of a form: Is it possible to Post a Form by an Id?

I have a CommandButton (PrimeFaces 3.0.1), that is not in a Form-Tag. Is there a way to reference a form via it's Id or something, so that the Button can post a Form even when it's not a child-element of that Form? I did'nt find any apropriate in the specs...

The Button currently looks like this:

<p:commandButton value="#{messages['000.label.ok']}"
         oncomplete="w00001.hide()" type="submit" 
                 action="cancel"
         ajax="false" immediate="true" />

After pressing the Button JSF forwards to another page, that is mapped by the key "cancel".

Thanks!!!

Upvotes: 4

Views: 10109

Answers (2)

Nwawel A Iroume
Nwawel A Iroume

Reputation: 1369

According to https://stackoverflow.com/a/11766570/3568831 You cannot process another from by a submit of one form you should use <p:remoteCommand /> as explained here and also with this link to see an example

Upvotes: 0

Mario B
Mario B

Reputation: 2337

edit1 So what you want to do is submit a form, then redirect. As said before, commandButton has to be inside a form if you want to use it for submit, but it doesn't has to be the same form. You can wrap a second form around commandButton, and still use it to submit the values of your first form.

        <h:form id="form_test">
            <!-- values of this form will be submitted -->
        </h:form>
        <h:form>
            <p:commandButton value="cancel" action="#{yourBean.cancelFlow}"
                process="form_test @this" ajax="false" />
        </h:form>

the Method in YourBean.java could look like this (assuming your cancelpage.xhtml is in the package your.package)

public String cancelFlow() {
    // do something with form data

    return "/your/package/cancelpage?faces-redirect=true";
}

You could also redirect programmatically, using FacesContext.getCurrentInstance().getExternalContext().redirect

Upvotes: 4

Related Questions