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:

      public class Bean {
        private String password1;
        private String password2;

        I tried to create a Validator using the Method:

      public boolean isValid(Object obj) {

        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 ...")
               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:messages />
                  <h:panelGrid columns="2">
                     <h:outputText value="Password: "/>
                     <h:inputText value="#{user.password}" />
                     <h:outputText value="Password2: "/>
                     <h:inputText value="#{user.password2}" />
               <div align="center">
                  <h:commandButton action="#{userMB.add}" value="Add" />

            My entity:

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

            My mbean:

            public class UserMB implements Serializable
               @In(create=true) @Out
               private User user;
               // This is a EJB - Session Bean
               private UserSB userSB;
               public String add(){
                  // This EJB method just do a 'em.persist(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.

              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.