1 Reply Latest reply on Mar 30, 2009 5:53 PM by norman

    validate multiple fields

    kretes
      I wanna validate two fields on some logic. - jest computing a diff between the dates.

      I'm using hibernate validator for validation along with s:validateAll, so I've added a transient field on the entity that would compute the value from two fields and return an int, and I've also added a Min(1):
      @Transient
              @Min(1)
              public Integer getInterval() {
                      if (from == null || to == null) {
                              return 0;
                      } else {
                              return Days.daysBetween(new DateTime(from.getTime()),
                                              new DateTime(to.getTime())).getDays();
                      }
              }

      on the jsf side I have inputs for 'from' and 'to' fields only.

      The validation isn't processed at the page request, but on the persist method of entity, and then it throws the exception:
      Caused by org.hibernate.validator.InvalidStateException with message: "validation failed for: seam.entity.Leave"

      org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148)
      org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
      org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:142)
      org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
      org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
      org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
      org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
      org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
      org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
      org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
      org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
      org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
      org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
      org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
      org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
      org.jboss.seam.persistence.EntityManagerProxy.persist(EntityManagerProxy.java:137)
      org.jboss.seam.framework.EntityHome.persist(EntityHome.java:84)

      So I checked a solution of marking method with hib validator @AssertTrue:
      @AssertTrue
              public boolean validate() {
                      return getInterval() > 0;
              }

      but it also is checked only at the persist method.. :(

      I also created a Type validator for that gives the same problem - not invoked on page request, but on persist -> exception.

      <Other hibernate validations like @Future works>

      I assume my other validators aren't invoked because there is no input for them. adding a hidden field for transient field was not a success because it was invoked once at the first page render (and then resulted in value 0, so not valid), and it didn't let me create an entity, because it didn't recheck the value by invoking transient method again on next requests.

      Any idea on how to perform two-field validation? I though seam would be a little better than standard jsf validation
        • 1. Re: validate multiple fields
          norman

          It wouldn't make sense to run all validators each time, but I agree that there ought to be a better way to add additional validators in.  I think you should run the particular validators you want in whatever action gets invoked when you submit your form.m You can get the validator for a class using org.jboss.seam.core.validators.