0
votes

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

3
When do you need to display the message. When the form is submitted? - prageeth
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? - Xtreme Biker

3 Answers

1
votes

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> 
0
votes

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"));
}
0
votes

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