-
1. Re: Custom Validator using two fields
tom_goring May 8, 2008 5:43 PM (in response to mthiago)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
mthiago May 8, 2008 7:16 PM (in response to mthiago)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
nathandennis Jun 24, 2008 8:25 PM (in response to mthiago)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."));
-
4. Re: Custom Validator using two fields
dhinojosa Jun 25, 2008 12:42 AM (in response to mthiago)Easier, use someone else's stuff. ;)
http://www.seamframework.org/Community/StepByStepHowToUseApacheTomahawkTagsInYourSeamFaceletsApplication -
5. Re: Custom Validator using two fields
mthiago Jun 25, 2008 1:19 PM (in response to mthiago)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.