8 Replies Latest reply on Mar 25, 2008 10:36 PM by t1mb0

    Accessing Seam components from a custom JSF Validator?

    t1mb0

      I have a custom JSF validator (which does have @Name) from which I want to access a seam component (also a stateless session bean), which itself uses a seam injected PersistenceContext.


      @Name("usernameValidator")
      public class UsernameValidator implements Validator
      {
          @In(create=true)
          AccountDao accountDao;
      
          public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
          {
              // validation logic
          }
      }
      



      However the accountDao reference is null when the validator is executed.  I'm assuming this is because the validator is invoked as a POJO by the JSF lifecycle and seam is not involved. So how can I access Seam components and take advantage of a seam injected persistence context?


      Any help much appreciated.
      Cheers,
      Tim

        • 1. Re: Accessing Seam components from a custom JSF Validator?
          t1mb0

          I now feel a little daft as I've just spotted this in the docs (3.2.1)



          Seam stateless session bean components may be instantiated using
          Component.getInstance()


          or
          @In(create=true)




          However I still wonder is it possible to have JSF resolve the above mentioned JSF validator as seam component usernameValidator without having to make it a hibernate validator? (hibernate seems to validate all the way to the database, but sometimes you just want validation on the front end only)

          • 2. Re: Accessing Seam components from a custom JSF Validator?
            t1mb0

            Now I'm seeing that the hibernate validator fails to validate whenever I add a custom JSF validator.  If I comment out the JSF validator, then the hibernate validator will correct flag the field as invalid and the message is displayed.


            Is there known issue issue where JSF validators effectively mask the hibernate validator? Can the two types of validator not co-exist?


            I'm pulling my hair out with these validators so if anyone out there can explain this I'd be really grateful.


            Thanks


            Tim

            • 3. Re: Accessing Seam components from a custom JSF Validator?
              msystems

              I'm using JSF validators and Hibernate validators and it works fine for me - no problems.


              BTW: have you tried:


              @Name("usernameValidator")
              @Validator
              public class UsernameValidator implements javax.faces.validator.Validator
              {
                  @In(create=true)
                  AccountDao accountDao;
              
                  public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
                  {
                      // validation logic
                  }
              }
              



              Remember to remove the configuration/declaration of the JSF validator from the faces-config.xml file.

              • 4. Re: Accessing Seam components from a custom JSF Validator?
                matt.drees

                IIRC, if you use s:validateAll, a model validator (Hibernate validator) will be attached only if another JSF validator isn't explicitly used.  So, if you need both a model validator and another jsf validator, you should explicitly use s:validate.

                • 5. Re: Accessing Seam components from a custom JSF Validator?
                  barbacena

                  Hi Tim,


                  There is a Jira issue about this, then I don't recomend you using a @In on any validator or converter.


                  Use:


                  @Name("usernameValidator")
                  @Validator
                  @BypassInterceptors
                  public class UsernameValidator implements javax.faces.validator.Validator
                  {
                      
                      AccountDao accountDao = Component.getInstance("accountDao");
                  
                      public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
                      {
                          // validation logic
                      }
                  }
                  



                  IMHO, that's ugly, but that is the way they are going ... I think that is because they are trying to untie the seam and JSF.


                  • 6. Re: Accessing Seam components from a custom JSF Validator?
                    barbacena

                    Hi Matt,


                    Isn't that a bug? IMHO, the behavior of s:validateAll should be the same of s:validate applied to every field.

                    • 7. Re: Accessing Seam components from a custom JSF Validator?
                      matt.drees

                      No, it's definitely intentional.  I forget exactly why, but I think it's discussed in previous forum posts.


                      However, this behavior isn't documented well in the reference docs (if at all; can't remember).  It'd probably be worth a jira issue for that.

                      • 8. Re: Accessing Seam components from a custom JSF Validator?
                        t1mb0

                        Thanks to everyone who answered me on this..  All time and input is much appreciated!


                        In summary :-


                        I can confirm that using s:validate rather than s:validateAll does allow both JSF and hibernate validators to work together .. thanks for the tip Matt!


                        And pure JSF validators should not attempt to be Seam components themselves and should manually lookup or create a seam component if necessary.


                        This is my first seam application and already I can see its power as an integration framework. I will definitely be a frequenter of this site!


                        Thanks again,


                        Tim