Wednesday, October 31, 2012

Re: Cross field validation on client

I am not familiar with the GWT Validation framework.
I'm not really sure why people use it considering vanilla GWT supports JSR303 (the documentation is pretty bad admittedly).

Using plain GWT you would have a class-level validation annotation:

@Target(ElementType.TYPE)  @Retention(RetentionPolicy.RUNTIME)  @Constraint(validatedBy = PasswordsEqualValidator.class)  @Documented  public @interface PasswordsEqual {                    String DEFAULT_MESSAGE = "Passwords must be the same";            String message() default DEFAULT_MESSAGE;                    Class<?>[] groups() default {};            Class<? extends Payload>[] payload() default {};  }

Then you would need a custom validator. Mine looks like this:

public class PasswordsEqualValidator implements ConstraintValidator<PasswordsEqual, CreateAccountBean> {            @Override          public void initialize(PasswordsEqual constraintAnnotation) {}                    @Override          public boolean isValid(CreateAccountBean value, ConstraintValidatorContext context) {                                    if( value == null )                          return true;                                    String password = value.getPassword();                  String confirmPassword = value.getConfirmPassword();                                    if( password == null && confirmPassword == null )                          return true;                                    if( password != null && password.equals(confirmPassword) )                          return true;                                    return false;          }  }

Then annotate the javabean class being validated with @PasswordsEqual

This works on the client and server.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/X7QzUe1UblcJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment