1 Reply Latest reply: Apr 26, 2011 6:20 AM by 857716 RSS

    Custom validator not being found

    849717
      Hi Forum,

      I searched everywhere and couldn't find anybody who has received the following message when attempting to register a custom validator in JSF 2.

      +<f:validator> A validator id was not specified. Typically the validator id is set in the constructor ValidateHandler(ValidatorConfig)+

      Using Mojarra 2.0.3 under Tomcat 7. Here is my validator:
      package com.company.validators;
      
      import javax.faces.application.FacesMessage;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.validator.FacesValidator;
      import javax.faces.validator.Validator;
      import javax.faces.validator.ValidatorException;
      
      import org.apache.log4j.Logger;
      
      import com.company.util.StringUtils;
      
      @FacesValidator("com.company.validators.PhoneValidator")
      public class PhoneValidator implements Validator {
      
           private Logger log = Logger.getLogger(this.getClass());
           
           @Override
           public void validate(FacesContext context, UIComponent component,
                     Object value) throws ValidatorException {
                try {
                     log.info("Starting PhoneValidator.validate");
                     if (!StringUtils.isValidPhoneNumber((String)value)) {
                          FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Requestor Phone Number is an invalid format.", null);
                          throw new ValidatorException(msg);
                     }
                } finally {
                     log.info("Ending validateRequestorPhone");
                }
           }
      }
      As you can see I gave it the proper annotation, I assume that by doing that I don't need to register it in faces-config.xml right?

      Here is my markup:
      <h:outputText value="Requestor Phone: *" />
                               <h:inputText required="true" label="requestorPhone"
                                         value="#{scheduleVisit.visit.requestorPhone}">
                                    <f:validator validatorId="com.company.validators.PhoneValidator" />
                               </h:inputText>
                               <h:outputText value="(000-000-0000 or 000-000-0000EXT0000)" />
      As you can see in my markup, the validatorId attribute matches exactly the validatorid declared in the validator class. The validator class is on the classpath. Further, Eclipse cannot locate this validator either and provides a warning, I assume because it is stupid and looks for it in the faces-config.xml.

      I read a podunk blog article that stated to do it this way. Perhaps this article is full of baloney?

      Edited by: 846714 on Mar 23, 2011 6:58 AM
        • 1. Re: Custom validator not being found
          857716
          Hi,

          I have a similar problem with Eclipse and validators regsitration.

          I have registered the validator with the annotation @FacesValidator correctly, but Eclipse launch a warning saying it isn't registered.

          And when a run the application, I get an error saying that an instance for the validator can't be created.

          On the other hand, with the converters, Eclipse launch a similar warning saying they are not registered, but then the application runs well.

          ¿Anyone knows why this happen? ¿Is it a bug? ¿Why do I have to register the validator in the faces-config file and it doesn't work with annotation?

          Thanks in advanced.

          [I'm from Spain, sorry if my english isn't very good]