繁体   English   中英

具有多个命令按钮的JSF Primefaces必需值问题

[英]JSF Primefaces required value issue with multiple commandbutton

我的问题是我在xhtml页面中有多个PrimefacescommandButton必需的消息和验证器对它们都起作用。 这不是我想要的。 我希望所需的消息和验证器仅在单击ID为“ sendform ”的命令按钮sendform

<p:growl id="growl" life="6000" />

    <p:inputText id="fullName" 
                 value="#{messageBean.fullName}" 
                 required="true" 
                 requiredMessage="You need to enter your full name to apply this form." 
                 styleClass="contacttext"/>

    <p:inputText id="email" 
                 value="#{messageBean.email}" 
                 required="true" 
                 requiredMessage="You need to enter your email to apply this form." 
                 validatorMessage="Invalid e-mail format">
                        <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" /> 
    </p:inputText>

    <p:commandButton id="sendform" 
                     value="#{msg['sendUs']}" 
                     actionListener="#{messageBean.sendMessage}" 
                     update="growl fullName email textarea" 
                     styleClass="sendus"/>

<p:commandButton id ="otherStuff" 
                 value="otherStuff" 
                 actionlistener="#{someBean.someWork}" />
<p:commandButton id ="anotherStuff" 
                 value="anotherStuff" 
                 actionlistener="#{anotherBean.anotherWork}" />

如何解决该问题,以便仅在此命令按钮上运行必需的消息?

 <p:commandButton id="sendform" 
                  value="#{msg['sendUs']}" 
                  actionListener="#{messageBean.sendMessage}" 
                  update="growl fullName email textarea" 
                  styleClass="sendus"/>

在每个命令按钮上使用process属性,以指定单击按钮时应处理的组件。 根据您的示例代码,一些基本设置将是保持sendform按钮sendform ,并将process="@this"otherStuffanotherStuff按钮。 如果需要处理其他任何内容,只需将组件ID添加到process属性。

每当按下相关按钮时,跳过对所有字段的验证。

您可以实现以下代码。

<p:inputText id="fullName" required="#{not empty param[sendForm.clientId]}" />

<p:inputText id="email" required="#{not empty param[sendForm.clientId]}" />

<p:commandButton id="sendform" 
                 binding="#{sendForm}"
                 value="#{msg['sendUs']}" 
                 actionListener="#{messageBean.sendMessage}" 
                 update="growl fullName email textarea" 
                 styleClass="sendus"/>
<p:commandButton id ="otherStuff" 
             value="otherStuff" 
             actionlistener="#{someBean.someWork}" />
<p:commandButton id ="anotherStuff" 
             value="anotherStuff" 
             actionlistener="#{anotherBean.anotherWork}" />

您可以从传奇的BalusC中看到另一个示例,该示例位于如何让验证取决于按下的按钮?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM