2 Replies Latest reply on Feb 17, 2009 2:43 PM by nkr1pt

    HtmlPickList validator only executed when something selected

    nkr1pt

      Hi,

      I have defined and created a custom validator on an HtmlPickList, but the
      problem is that the validator gets called only if there has been interaction with the picklist (eg: item selected)

      <rich:pickList id="usersPickList"
       value="#{selectUsersModuleBean.targetUsers}"
       showButtonsLabel="false"
       converter="UserConverter"
       rendered="false">
       <f:validator validatorId="RecipientsValidator" />
       <f:selectItems value="#{selectUsersModuleBean.users}" />
       </rich:pickList>
      


      public class RecipientsValidator implements Validator {
      
       @Override
       public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
       if ((context == null) || (component == null)) {
       throw new IllegalArgumentException(context == null ? "Context" : "Component" + " cannot be null");
       }
      
       List recipients = (List)value;
       if (recipients.size() < 1) {
       throw new ValidatorException(new FacesMessage("'Recipients' is required"));
       }
       }
      
      }
      


      Any ideas?

      Thx

        • 1. Re: HtmlPickList validator only executed when something sele
          nkr1pt

          As a fix I'm doing the validation myself in the send action, instead of letting the processing lifecycle handle it, like this:

          public String send() {
           try {
           selectUsersModule.validate();
           findRecipients();
           EmailMessage mail = new EmailMessage("someone@somewhere.com", recipients, title, message);
           notificationSender.send(mail);
           } catch (ValidatorException ve) {
           FacesContext context = FacesContext.getCurrentInstance();
           context.addMessage(ve.getMessage(), ve.getFacesMessage());
           }
          
           return null;
           }
          


          this certainly works, but I'm not sure if it is a good way to handle it...
          I'm open for better solutions.

          Gr

          • 2. Re: HtmlPickList validator only executed when something sele
            nkr1pt

            I can offcourse set the required attribute of picklist to true...

            stupid :)