5 Replies Latest reply on Jun 25, 2008 1:19 PM by Thiago Meneghello

    Custom Validator using two fields

    Thiago Meneghello Newbie

        Is there a way to create a Validator comparing two fields.


        I was thinking in something like this:



      @Name("myBean")
      public class Bean {
      
        @ValidateEquals(field="#{myBean.password2}")
        private String password1;
      
        private String password2;
      
        ...
      
      }
      



        I tried to create a Validator using the Method:


      ...
      public boolean isValid(Object obj) {
         org.jboss.seam.Component.getInstance("myBean");
         ...
      }
      
        
      



        But the instance has the password1 and password2 fields null.

        • 1. Re: Custom Validator using two fields
          Tom Goring Apprentice

          If it's a hibernate validator thing (on an entity) you can use @AssertTrue :



               @AssertTrue(message="Msg ...")
               @Transient
               public boolean isValidxyz() {
          ... check your 2 fields here
                    return true;
               }
          
          



          • 2. Re: Custom Validator using two fields
            Thiago Meneghello Newbie

              I wanted to do this validation on the MBean Fields too.


              Is there some problem to use the Hibernate Validation on MBean Fields? Because the tests that I made works perfectly.


              Other thing, if I do this validation on Entity and input different passwords, I got an exception not handled by Seam validateAll component.


            [arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@18eedc2
            javax.persistence.PersistenceException: org.hibernate.validator.InvalidStateException: validation failed for: entity.User
                 at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:527)
                 at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:114)
                 at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:247)
                 at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:86)
            ...
            




              Ex: Here is my form:


            <h:form>
            <div>
               <div>
                  <h:messages />
               </div>
               <s:validateAll>
                  <h:panelGrid columns="2">
                     <h:outputText value="Password: "/>
                     <h:inputText value="#{user.password}" />
                     <h:outputText value="Password2: "/>
                     <h:inputText value="#{user.password2}" />
                  </h:panelGrid>
               </s:validateAll>
               </div>
               <div align="center">
                  <h:commandButton action="#{userMB.add}" value="Add" />
               </div>
            </div>
            </h:form>
            



            My entity:


            @Entity
            @Name("user")
            @Table( ... )
            public class User implements java.io.Serializable {
            
               private int id;
               private String password;
               private String password2;
            
               ...
            
               public String getPassword(){
                  return password;
               }
            
               @Transient
               public String getPassword2(){
                  return password2;
               }
            
               @AssertTrue(message="The passwords must be equals")
               @Transient
               public boolean isValidPassword(){
                  if(password != null) {
                     return password.equals(password2);
                  }
                  return true;
               }
            }
            



            My mbean:


            @Name("userMB")
            @Scope(ScopeType.SESSION)
            public class UserMB implements Serializable
            {
               ...
            
               @In(create=true) @Out
               private User user;
            
               // This is a EJB - Session Bean
               @In(create=true)
               private UserSB userSB;
            
               public String add(){
                  // This EJB method just do a 'em.persist(user)';
                  userSB.add(user);
               }
            }
            



            Do you know what else I have to do?

            • 3. Re: Custom Validator using two fields
              nathan dennis Expert

              just create a custom validator and call it using f:validate or s:validate... dont put that mess in your entity. you might want to use it for some other action later where you dont want all the equals this equals that in there.


              
              @Name("passwordValidator")
              @Validator
              @Transactional
              public class PasswordValidator implements javax.faces.validator.Validator, Serializable{
              .....
              throw new ValidatorException(new FacesMessage("Passwords do not match."));
              

              • 5. Re: Custom Validator using two fields
                Thiago Meneghello Newbie

                Very good this Tomahawk.


                I will study a little more about this and I think that I will use this framework in my applications.


                Thank you.