0

I am creating a selectonemenu using Primefaces and JSF. I want to appear a message saying "Choose one of the options". How can I do that?

This is the code:

  <!--Gender-->
           <p:selectOneMenu id="gender" value="#{users.gender}" required="true"
                               requiredMessage="Choose one of the options">
            <f:selectItem itemLabel="Choose gender" itemValue="#{null}" />
             <f:selectItem itemLabel="Male" itemValue="Male" />
            <f:selectItem itemLabel="Female" itemValue="Female" />
          </p:selectOneMenu>

Thanks

william
  • 103
  • 1
  • 2
  • 9
  • When do you need to display the message. When the form is submitted? – prageeth Sep 15 '14 at 08:41
  • Please, better post your code here in the thread, instead of providing an external link to it. Having said that, which is the behaviour with your current code? Which PF version are you using? – Aritz Sep 15 '14 at 09:17

3 Answers3

1

adding below on the submit button will work fine as well. ajax="false"

*********************edited**********
As you requested, please find below block of codes that works fine for me  ::                                                                                                                                                                                                    

<h:form>
<p:panelGrid  style="margin-top:5px;width:730px;">
 <f:facet name="header">
   <p:row >
    <p:column colspan="20">Select Company</p:column>
   </p:row>
 </f:facet>

 <p:row>
   <p:column>
     <p:selectOneMenu id="companyMenu" value="#{clientList.clientCode}" 
           style="width:550px;margin:5px;text-color:black" required="true" 
                               requiredMessage="Please select a company !">
       <f:selectItem itemLabel="Select a Company" noSelectionOption="true" 
                                                 itemValue="#{null}" />
       <f:selectItems value="#{clientList.clients.entrySet()}" var="entry"  
                           itemValue="#{entry.key}" itemLabel="#{entry.value}" >
       </f:selectItems>
     </p:selectOneMenu>
   </p:column>

   <p:column>
     <p:commandButton value="Submit" style="margin:5px;" action="#
            {viewPayment.companyDetails(clientList.clientCode)}" ajax="false"/>
   </p:column>
 </p:row>

</p:panelGrid>

</h:form> 
A.Akher
  • 13
  • 4
  • @kwoxer please find above the *****edited part for exact code without any but added ajax="false" at – A.Akher May 28 '17 at 10:23
0

You can use :

if (users.getGender()==null) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Choose one of the options",
                            "Choose one of the options"));
}
S19
  • 71
  • 1
  • 5
0

SOLVED.I forgot to add the line <p:message for="gender" /> to the code. Thanks.

william
  • 103
  • 1
  • 2
  • 9