2

我正在尝试使用 jsf 2.0(primefaces)实现 ajax 客户端验证。我更喜欢 onblur 事件而不是 ontype,因为它看起来对用户更友好。问题是:当焦点位于无效字段内时,我的注册按钮必须单击两次才能正常工作。我们第一次点击 - onblur 事件发生,第二次点击 - 提交发生(我认为这是原因)。我的代码:

    <html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <ui:composition template="template.xhtml">
        <ui:define name="content">
    <p:panel header="#{msg['register']}" style="width: 550px; align: left;">
        <h:form>
            <h:panelGrid columns="3" cellpadding="2">             

                <h:outputLabel for="name" value="#{msg['yourName']}:" />  
                <p:inputText id="name" value="#{userManagedBean.name}">  
                <f:validateLength maximum="20" />  
                <p:ajax update="msgName" event="blur" />  
                </p:inputText>  
                <p:message for="name" id="msgName" display="text"/>

                <h:outputLabel for="userName" value="*#{msg['login']}:"/>
                <p:inputText id="userName" value="#{userManagedBean.username}" label="UserName" required="true"> 
                <f:validateLength minimum="5" maximum="20" for="userName"/>
                <p:ajax update="msgUserName" event="blur"/>
                </p:inputText>
                <p:message for="userName" id="msgUserName"/>

                <h:outputLabel for="password" value="*#{msg['password']}:" />
                <p:password id="password" value="#{userManagedBean.password}" required="true">
                <f:validator validatorId="confirmPasswordValidator" />
                <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
                <f:ajax event="blur" execute="password confirm" render="m_password" />
                </p:password>
                <p:message id="m_password" for="password" />

                <h:outputLabel for="confirm" value="*#{msg['retypePassword']}:" />
                <p:password id="confirm" binding="#{confirmPassword}" required="true">
                <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" />
                </p:password>
                <p:message id="m_confirm" for="confirm" />

                <h:outputLabel for="email" value="#{msg['email']}:"/>
                <p:inputText id="email" value="#{userManagedBean.email}" label="email">                
                <f:validateRegex pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" for="email"/>
                <p:ajax update="msgEmail" event="blur"/>
                </p:inputText>
                <p:message for="email" id="msgEmail"/>

           </h:panelGrid>
                <ui:remove><p:captcha label="Captcha"/> </ui:remove>
                <h:commandButton type="submit" value="#{msg['register']}" action="#{userManagedBean.register}"></h:commandButton>                          
        </h:form>
    </p:panel>  
</ui:define>
</ui:composition>
</html>
4

0 回答 0