GWT JSR303 Validation, validate method OR use custom annotations

chriscross

I'm trying to use GWT's 2.5 in-build validation feature. I have a few complex validations. Cross field validation with Hibernate Validator (JSR 303) suggests that I could either include methods which do the validation OR write my own annotations. However, both don't work.

public class PageData extends Serializable
    @NotNull(message="Cannot be null!")
    Boolean value

    @AssertTrue(message="isValid() is false!")
    private boolean isValid() {
        return false;
    }

    //Getters and Setters
}

Boolean value is validated. However, isValid() is never called/validated. But why? Is this a GWt specific problem?

Then I tried to write my own annotation, The @FieldMatch example in Cross field validation with Hibernate Validator (JSR 303) uses Beans.getProperty() from Apache Commons BeanUtils, which I cannot use in GWT. Is there any way to make these kind of complex annotations work in GWT?

koma

Here is how I created a custom validation that works across multiple fields of one bean. It checks that when the field ContactProfile for a Contact bean is set to COMPANY, then company name must be filled out, otherwise when set to PERSON, the first name or last name must be 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 I can set

@ValidCompanyOrPerson
public class Contact  {
 ...
}

I can use this validation both client (GWT) and server side.

Hope that helps ....

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSR303 validation doesn't validate if @Id is not @GeneratedValue with Spring Data Jpa and Hibernate

From Dev

validate custom method with jQuery validation

From Dev

JSR303 custom validators being called twice

From Dev

Custom Bean Validator -- JSR303/JSR349 can Validator choose message when invalid?

From Dev

Can JSR-349 Bean Validation Annotations be Used to Validate Java Time Types?

From Dev

Better way of using JSR303 annotations in Spring MVC to check whitespaces only

From Dev

Use validate method and validation xml together in Struts 1

From Dev

How to break coupling between bean and validator with Bean Validation (JSR303)?

From Dev

Inline validation using JSR-303 in GWT, part 2: A good implementation?

From Dev

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

From Dev

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

From Dev

How to use Data Annotations to validate a nullable int

From Dev

How to use Data Annotations to validate a nullable int

From Dev

Custom Validation Method and Messages

From Dev

Validation with custom Data Annotations in conflict with localization change

From Dev

Custom validate method not working - jQuery validate plugin

From Dev

Validate.js custom method

From Dev

Add a custom validation using Angular Auto Validate

From Dev

Error when validate entity with custom validation constraint

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

How to use Hibernate validation annotations with enums?

From Dev

rails 4 custom validation method

From Dev

JQuery validation custom method trouble

From Dev

custom validation method and rspec tests

From Dev

Use Android Annotations in custom dialog class

From Dev

How to use custom type annotations in Java

From Dev

Use Android Annotations in custom dialog class

From Dev

Use custom annotations in Symfony User Provider

Related Related

  1. 1

    JSR303 validation doesn't validate if @Id is not @GeneratedValue with Spring Data Jpa and Hibernate

  2. 2

    validate custom method with jQuery validation

  3. 3

    JSR303 custom validators being called twice

  4. 4

    Custom Bean Validator -- JSR303/JSR349 can Validator choose message when invalid?

  5. 5

    Can JSR-349 Bean Validation Annotations be Used to Validate Java Time Types?

  6. 6

    Better way of using JSR303 annotations in Spring MVC to check whitespaces only

  7. 7

    Use validate method and validation xml together in Struts 1

  8. 8

    How to break coupling between bean and validator with Bean Validation (JSR303)?

  9. 9

    Inline validation using JSR-303 in GWT, part 2: A good implementation?

  10. 10

    Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

  11. 11

    Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

  12. 12

    How to use Data Annotations to validate a nullable int

  13. 13

    How to use Data Annotations to validate a nullable int

  14. 14

    Custom Validation Method and Messages

  15. 15

    Validation with custom Data Annotations in conflict with localization change

  16. 16

    Custom validate method not working - jQuery validate plugin

  17. 17

    Validate.js custom method

  18. 18

    Add a custom validation using Angular Auto Validate

  19. 19

    Error when validate entity with custom validation constraint

  20. 20

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  21. 21

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  22. 22

    How to use Hibernate validation annotations with enums?

  23. 23

    rails 4 custom validation method

  24. 24

    JQuery validation custom method trouble

  25. 25

    custom validation method and rspec tests

  26. 26

    Use Android Annotations in custom dialog class

  27. 27

    How to use custom type annotations in Java

  28. 28

    Use Android Annotations in custom dialog class

  29. 29

    Use custom annotations in Symfony User Provider

HotTag

Archive