如果inputText验证失败或启动ajax调用,则取消激活commandButton功能

Mur*_*zen 13 validation jsf primefaces jsf-2

我在表单中有以下代码:

<p:panel id="panel" header="lalalala">
    <h:panelGrid columns="5">

        <h:outputLabel value="Enter name" for="name" />

        <p:inputText id="name" value="#{userBean.name}"
            required="true" label="name" maxlength="15"
            validatorMessage="oops"
            requiredMessage="ooopps">
            <f:validateRegex
                pattern="^somepattern$">
            </f:validateRegex>
            <p:ajax event="blur" update="inputValidationMessage" />
        </p:inputText>

        <p:message id="inputValidationMessage" showSummary="false"
            for="name" />
        <p:watermark for="name" value="eg. John" />

    </h:panelGrid>

    <p:commandButton id="submitButton" value="Submit" update="panel"
        actionListener="#{userBean.save}"
        onclick="handleOnclick"
        oncomplete="handleAjaxResponse(xhr, status, args)">
    </p:commandButton>
    <p:messages autoUpdate="true" globalOnly="true" showDetail="true" />
</p:panel>
Run Code Online (Sandbox Code Playgroud)

因此验证消息在inputText元素上运行良好.但是,即使输入无效,提交按钮仍然会在单击时发出Ajax请求.如果inputText有效,我希望发送Ajax请求.

我怎么做?

此外; 还有一种情况我想停用按钮.这是验证inputText并且用户单击该按钮的情况.这个的原因是,我不希望用户错误地泛滥请求.(即反复按提交按钮)

目前,handleOnclickjavascript函数只显示一个模式对话框,说"等待".由于该对话框是模态的,因此背景无法访问.但是,这仍然不能阻止用户反复按下它,因为在onclick和模态对话框显示之间经过1秒.此外; 这种方法不会阻止提交无效数据.你能提出什么建议?

我使用JSF2.0和Primefaces 3.0.

任何帮助赞赏.

Bal*_*usC 18

您可以使用按钮的disabled属性.只需将其设置为true每当FacesContext#isPostback()返回false(因此,它是初始请求),或者当它true(因此,表单提交已经发生)时,然后检查是否FacesContext#isValidationFailed()返回true(因此验证通常失败).

<p:commandButton ... disabled="#{not facesContext.postback or facesContext.validationFailed}" />
Run Code Online (Sandbox Code Playgroud)

您只需要确保更新ajax请求上的按钮,以便在必要时重新启用或启用它.

<p:ajax event="blur" update="inputValidationMessage submitButton" />
Run Code Online (Sandbox Code Playgroud)