【问题标题】:JSF Primefaces required value issue with multiple commandbuttonJSF Primefaces 需要多个命令按钮的值问题
【发布时间】:2015-01-09 02:11:53
【问题描述】:

我的问题是我在一个 xhtml 页面中有多个PrimefacescommandButton。我需要的消息和验证器适用于它们中的每一个。这不是我想要的。我希望只有当我单击 ID 为“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"/>

【问题讨论】:

    标签: jsf primefaces jsf-2.2


    【解决方案1】:

    在按下相关按钮时跳过所有字段的验证。

    你可以实现如下代码。

    <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}" />
    

    你可以在How to let validation depend on the pressed button?看到另一个传奇BalusC的例子

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢@Predrag Maric,我学习了primefaces的流程并为jsf执行
      猜你喜欢
      • 2017-10-04
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2014-12-31
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多