Вопрос:

How to stay on same Tab if field evalution failed

jsf primefaces tabs

1091 просмотра

1 ответ

167 Репутация автора

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.

index.xhtml

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

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

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 Источник Размещён: 17.02.2015 03:51

Ответы (1)


3 плюса

10105 Репутация автора

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">
    ...
</p:tabView>

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"
        actionListener="#{controller.saveA}"
        icon="ui-icon-check" validateClient="true" style="float-right"
        oncomplete="if (args &amp;&amp; !args.validationFailed) tabWidget.select(0);"
    />
    

    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

    RequestContext.getCurrentInstance().execute("tabWidget.select(0)");
    
Автор: Kukeltje Размещён: 17.02.2015 04:09
Вопросы из категории :
32x32