简体   繁体   中英

Primefaces selectonemenu requiredmessage

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

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> 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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