1

My problem is I have more than one Primefaces's commandButton in an xhtml page.My required messages and validators work for each of them. This is not what I desire. I want that the required messages and validators should work only when I clicked the commandbutton with the id is "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}" />

How can I fix it up to run required messages only this commandbutton?

 <p:commandButton id="sendform" 
                  value="#{msg['sendUs']}" 
                  actionListener="#{messageBean.sendMessage}" 
                  update="growl fullName email textarea" 
                  styleClass="sendus"/>
wittakarn
  • 3,124
  • 1
  • 18
  • 31
rLyLmZ
  • 495
  • 4
  • 21

2 Answers2

1

Use process attribute on each command button, to specify which components should be processed when the button is clicked. Some basic setup, based on your sample code, would be to leave the sendform button as it is, and to add process="@this" to otherStuff and anotherStuff buttons. Just add component ids to process attribute if anything else should be processed.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1

To skip the validation for all fields whenever the button in question is been pressed.

You can achieve as below code.

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

You can see another example from legendary BalusC at How to let validation depend on the pressed button?

Community
  • 1
  • 1
wittakarn
  • 3,124
  • 1
  • 18
  • 31