How to stay on same Tab if field evalution failed

jsf primefaces tabs

1091 просмотра

1 ответ

I have home tab and Validation tab. I like to validate user input and switch back to home tab if the user input is valid, otherwise stay on validation tab. At this point, it is switching back to home tab even for invalid user input after I click on submit button.


    <p:tab title="Home" titleStyleClass="repo">
        <h:panelGrid columns="2" cellpadding="10">
             /****home table code here***/

    <p:tab title="Validation">
            <h:panelGrid id="grid" columns="4" cellpadding="5">
                <h:outputLabel for="number" value="number:" />
                <p:inputText id="number" value="#{validationView.number}"
                    <f:validateDoubleRange minimum="100" maximum="200" />
        <p:commandButton value="Submit"
                actionListener="#{controller.saveA}" ajax="false"
                icon="ui-icon-check" validateClient="true" style="float-right" />

I am sure, I am missing something here, I don't know how to get back the validation result on a particular tab in primefaces and set tab switching based on that.

*I am using primefaces 5.1

Автор: SamK Источник Размещён: 13.11.2019 11:47

Ответы (1)

3 плюса

You have ajax="false" on your submit button. And since you do not use the activeIndex of the tabView, it wil always switch back to the first tab. Use ajax="true" (the default) instead of false. When removing the ajax="false", it should stay at the tab you are at. Also make sure you give your tab a widgetVar like

<p:tabView widgetVar="tabWidget">

You have two options then:

  1. in the oncomplete event of the commandButton, check for validationErrors and if non occured, switch to the first tab

    <p:commandButton value="Submit"
        icon="ui-icon-check" validateClient="true" style="float-right"
        oncomplete="if (args &amp;&amp; !args.validationFailed);"

    See for the why of the escaping of the & : Keep p:dialog open when a validation error occurs after submit

  2. You could also use the fact that IF your listener is called, you know that validation succeded. So in the listener you could use the remoteContext and make that execute the tab switch

Автор: Kukeltje Размещён: 17.02.2015 04:09
Вопросы из категории :