user2061913
user2061913

Reputation: 920

Validation preventing command buttons

Hi guys i have some input boxes where a user can enter details and submit it, this is requried to have some validation, not null etc this is working all fine, however i have a navigation button to go home if the user has come to this page by mistake or something, however when a user presses this button it also calls the validation and requires the user to enter details, which for a home button is no good, is there any way i can not have the validation run when a user presses this button ? it is a primefaces command button

here is the code, if i use a link i can navigate fine, but for user experience i would rather use a command button

            <h:form>
                <p:growl id="growl" showDetail="true" sticky="true" />  
                <!--ajax messages working now -->

                <p:panel header="Enter details">  
                    <h:panelGrid columns="2">
                        <h:outputLabel value="#{bundle.CreateUserdetailsLabel_id}" for="id" />
                        <h:inputText id="id" value="#{userdetailsController.selected.id}" title="#{bundle.CreateUserdetailsTitle_id}" required="true" requiredMessage="#{bundle.CreateUserdetailsRequiredMessage_id}"/>
                        <h:outputLabel value="#{bundle.CreateUserdetailsLabel_username}" for="username" />

                        <h:inputText id="UserName" value="#{userdetailsController.selected.username}" title="#{bundle.CreateUserdetailsTitle_username}" required="true" 
                                     requiredMessage="Username is required"             
                                     label="UserName" >
                            <f:validator validatorId="richard.validator.UserNameValidator" />
                        </h:inputText>

                    </h:panelGrid>
                    <br />
                    <p:commandButton  action="#{userdetailsController.create}" value="#{bundle.CreateUserdetailsSaveLink}" update="growl"/>

                    <br />
                    <br />

                    <p:commandButton action="#{userdetailsController.prepareList}" value="#{bundle.CreateUserdetailsShowAllLink}" immediate="true"/>
                </p:panel>
                <br />
                <br />


                <!--
                Trying to use a command button to navigate however it is requiring the user enter details first and this is not acceptable so looking for a solution

                <p:commandButton action="{naviagation.buttonHome}" value="Home" icon ="ui-icon-home"  ajax="False"/>
                                        <p:commandButton action="{bundle.ListUserdetailsIndexLink}" value="List of all current records" icon ="ui-icon-person"  ajax="False"/>
                -->  

                <h:link outcome="/index" value="#{bundle.ListUserdetailsIndexLink}"/>

            </h:form>

thanks

Upvotes: 1

Views: 139

Answers (2)

BalusC
BalusC

Reputation: 1108537

You're not supposed to submit a whole form and fire a POST request for sake of page-to-page navigation. This is not only wrong, but also bad for user experience and SEO.

Use <p:button> instead.

<p:button value="Home" outcome="/index" icon="ui-icon-home" />
<p:button value="List of all current records" outcome="/list" icon="ui-icon-person" />

If you really need to invoke an action (I'd rather pass a request parameter instead and have <f:event>/<f:viewAction> of the target view to perform business logic on it), then add process="@this" to <p:commandButton> to avoid the entire form being processed. Note that immediate="true" serves an entirely different purpose and should not be used for the specific of requirement bypassing the processing of the entire form.

Upvotes: 3

user2061913
user2061913

Reputation: 920

found the solution

Add immediate="true" to the command button which should skip processing of all input field which do not have the immediate="true" attribute.

Upvotes: 0

Related Questions