2 Replies Latest reply on Jun 5, 2009 5:17 PM by Alex Cougarman

    Validate blank selectOneMenu

    Alex Cougarman Apprentice

      Hi. I've got a selectOneMenu and I'd like to detect when the user has left it blank on form submit:

      <h:selectOneMenu id="lstRating" value="#{asset.rating}"
       required="true" requiredMessage="Rating is required.">
       <f:selectItems value="#{asset.ratingItems}" />
      </h:selectOneMenu>


      The selectOneMenu gets its value from this code; note that I'm setting the first Map object element to "" the empty string, but it's not detecting it as being empty on form submit:

      private Map<String, Object> ratingItem = null;
       private String rating;
      
       public Map<String, Object> getRatingItems(){
       if (ratingItem == null){
       ratingItem = new LinkedHashMap<String, Object>();
       ratingItem.put("","");
       ratingItem.put("TV-Y", "TV-Y");
       ratingItem.put("TV-Y7", "TV-Y7");
       ratingItem.put("TV-G", "TV-G");
       ratingItem.put("TV-PG", "TV-PG");
       ratingItem.put("TV-14", "TV-14");
       ratingItem.put("TV-MA", "TV-MA");
       }
       return ratingItem;
       }
      
       public String getRating(){
       return this.rating;
       }
       public void setRating(String rating){
       this.rating = rating;
       }


        • 1. Re: Validate blank selectOneMenu
          Alex Cougarman Apprentice

          Figured it out:

          public Map<String, Object> getRatingItems(){
           if (ratingItem == null){
           ratingItem = new LinkedHashMap<String, Object>();
           ratingItem.put("Please Select...",null);
           ratingItem.put("TV-Y", "TV-Y");
           ratingItem.put("TV-Y7", "TV-Y7");
           ratingItem.put("TV-G", "TV-G");
           ratingItem.put("TV-PG", "TV-PG");
           ratingItem.put("TV-14", "TV-14");
           ratingItem.put("TV-MA", "TV-MA");
           }
           return ratingItem;
           }


          and then also added the <rich:message> tag:

          <h:selectOneMenu id="lstRating" value="#{asset.rating}"
           required="true" requiredMessage="Rating is required.">
           <f:selectItems value="#{asset.ratingItems}" />
           </h:selectOneMenu>
           <rich:message for="lstRating" styleClass="errors" />


          • 2. Re: Validate blank selectOneMenu
            Alex Cougarman Apprentice

            Sorry. Jumped the gun. This is the code that works:

            private ArrayList<SelectItem> ratingItems = null;
            
             public ArrayList<SelectItem> getRatingItems(){
             ratingItems = new ArrayList<SelectItem>();
             ratingItems.add(new SelectItem(null,"Select One..."));
             ratingItems.add(new SelectItem("TV-Y", "TV-Y"));
             ratingItems.add(new SelectItem("TV-Y7", "TV-Y7"));
             ratingItems.add(new SelectItem("TV-G", "TV-G"));
             ratingItems.add(new SelectItem("TV-PG", "TV-PG"));
             ratingItems.add(new SelectItem("TV-14", "TV-14"));
             ratingItems.add(new SelectItem("TV-MA", "TV-MA"));
            
             return ratingItems;
             }