Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a command button without validating all required inputs?

I'm trying to make a redirection from index.xhtml to registerFirstTime.xhtml.

The code in the page index.xhtml is:

<h:form id="content" style="margin-top: 1em;">
    <p:panel id="panel" header="LOGIN">
        <p:messages id="msgs" globalOnly="true"/>
        <h:panelGrid columns="3">
            <h:outputLabel value="e-mail" />
            <p:inputText id="name" required="true" value="#{UserBean.email}"
                requiredMessage="Required: e-mail" display="icon">
            </p:inputText>
            <p:message id="msgName" for="name"/>
            <h:outputLabel value="Password" />
            <p:password id="password" required="true" value="#{UserBean.password}"
                requiredMessage="Required: Password" display="icon" feedback="false">
            </p:password>
            <p:message id="msgPassword" for="password"/>
        </h:panelGrid>
        <h:panelGrid columns="2">
            <p:commandButton value="Login" actionListener="#{UserBean.validate}" update="msgNombre, msgPassword, msgs"/>
            <h:commandButton value="Register for the first time" action="register"/>
        </h:panelGrid>
    </p:panel>
</h:form>

While, the content of the redirection, in faces-config.xml, is:

<navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>register</from-outcome>
        <to-view-id>registerFirstTime.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

Until the input name and password are filled, you can't do the redirect. How I can get do the redirection of the record without taking into account the required fields? Thanks! =)

like image 316
Víctor Pariente Avatar asked Jun 28 '12 11:06

Víctor Pariente


1 Answers

@BalusC solution should do the trick, as always. But I'd like to point out a couple of things since you seem to be using Primefaces. Both are unrelated to the question but might help you out at some point.

First, you could use implicit navigation (introduced in JSF2). That way you wouldn't need to define all the navigation rules in your faces-config.xml file (I work on an old JSF 1.2 project and hate the need to define the navigation roles for everything). Here's how you'd do it:

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true"/>

The faces-redirect parameter forces a redirect instead of a forward, in case you need that.

Also, say you want to properly process some values, but not all of them. In that case, you can use the process attribute of p:commandButton or p:ajax. For example

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true" process="@this, name"/>

This would make JSF process only the button (@this) and the component with id="name" (your e-mail field). Again, it probably doesn't apply to this question, but it's something I use often.

like image 56
Andre Avatar answered Oct 30 '22 01:10

Andre