3 Replies Latest reply on Jan 23, 2011 3:46 PM by nickarls

    Dependency Injection in JSF Validator

    devinderpal

      Does dependency injection suppose to work with JSF validators? These validators are container managed classes and hence container should be able to inject. my small program:

       

      directory variable is always null..

      ---------------

      ...

      @FacesValidator(value="com.cool.jsf.EmailValidator")

      public class EmailValidator implements Validator

      {

          @Inject

          private IDirectory directory;

       

          private static final String emailRegexPattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";

       

          @Override

          public void validate(FacesContext arg0, UIComponent arg1, Object arg2)

                  throws ValidatorException

          {

              String rawEmail = (String)arg2;

              Pattern p = Pattern.compile(emailRegexPattern, Pattern.CASE_INSENSITIVE);

              Matcher m = p.matcher(rawEmail);

              boolean validEmail = m.matches();

       

              if (!validEmail)

              {

                  FacesMessage msg = new FacesMessage("Invalid Email Address");

                  throw new ValidatorException(msg);

              }

       

              //now check if email already exists

              boolean emailExists = directory.doesEmailExist(rawEmail);

              if (emailExists)

              {

                  FacesMessage msg = new FacesMessage("Email Address already registered");

                  throw new ValidatorException(msg);

              }

          }

       

      }