GWT JSR303 Validation, validate method OR use custom annotations -


i'm trying use gwt's 2.5 in-build validation feature. have few complex validations. cross field validation hibernate validator (jsr 303) suggests either include methods validation or write own annotations. however, both don't work.

public class pagedata extends serializable     @notnull(message="cannot null!")     boolean value      @asserttrue(message="isvalid() false!")     private boolean isvalid() {         return false;     }      //getters , setters } 

boolean value validated. however, isvalid() never called/validated. why? gwt specific problem?

then tried write own annotation, @fieldmatch example in cross field validation hibernate validator (jsr 303) uses beans.getproperty() apache commons beanutils, cannot use in gwt. there way make these kind of complex annotations work in gwt?

here how created custom validation works across multiple fields of 1 bean. checks when field contactprofile contact bean set company, company name must filled out, otherwise when set person, first name or last name must filled out :

annotation definition :

@target({ elementtype.type }) @retention(retentionpolicy.runtime) @constraint(validatedby = validcompanyorpersonvalidator.class) public @interface validcompanyorperson {      string message() default "{contact.validcompanyorperson}";      class<?>[] groups() default {};      class<? extends contact>[] payload() default {}; } 

implementation :

public class validcompanyorpersonvalidator implements constraintvalidator<validcompanyorperson, contact> {      validcompanyorperson annotation;      public void initialize(validcompanyorperson annotation) {         this.annotation = annotation;     }      @suppresswarnings("nls")     public boolean isvalid(contact contact, constraintvalidatorcontext context) {         boolean ret = false;         if (contact.getcontactprofile() == null) {         } else if (contact.getcontactprofile().equals(contactprofile.company)) {             ret = (contact.getcompanyname() != null);         } else if (contact.getcontactprofile().equals(contactprofile.person)) {             ret = (contact.getgivenname() != null || contact.getfamilyname() != null);         }         return ret;     } } 

now can set

@validcompanyorperson public class contact  {  ... } 

i can use validation both client (gwt) , server side.

hope helps ....


Comments