How to call a command button without checking all the necessary inputs?

I am trying to redirect from index.xhtml to registerFirstTime.xhtml.

Code on index.xhtml page:

<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 redirect content 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 complete, you cannot redirect. How can I redirect a record without taking into account the required fields? Thank you =)

+7
source share
2 answers

Add immediate="true" to the command button, which should skip processing the entire input field that does not have the immediate="true" attribute.

 <h:commandButton value="Register for the first time" action="register" immediate="true" /> 

Unrelated to a specific problem, please note that you are not technically sending a redirect here. This basically sends a redirect, i.e. The URL in the address bar of the browser remains the URL of the login page. You need to add <redirect/> to <navigation-case> . Also note that navigation cases stay away from JSF 1.x. Since JSF 2.x you can do "implicit navigation" in which you simply specify the view identifier as a result.

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

Thus, you can completely get rid of the navigation case.

+26
source

@BalusC's solution should do the trick, as always. But I would like to point out a few things, since you seem to be using Primefaces. Both are not related to the question, but may help you at some point.

First, you can use implicit navigation (introduced in JSF2). This way you won’t need to define all the navigation rules in the faces-config.xml file (I’m working on an old JSF 1.2 project and hate the need to define navigation roles for everything). Here's how you do it:

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

The face redirection option forces redirection instead of redirection, if you need it.

Also, let's say you want to handle some values ​​correctly, but not all of them. In this case, you can use the process attribute for 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 force JSF to process only the button (@this) and the component with id = "name" (your email field). Again, this probably doesn't apply to the this question, but I often use it.

+6
source

All Articles