|
Validate
The validate support by GWTENT
Featured Using ValidateLinkshttp://jcp.org/en/jsr/detail?id=303 http://people.redhat.com/~ebernard/validation/ Add module inheritedAdd Validate module if you only using Validation functions in GWTENT <inherits name="com.gwtent.validate.Validate" /> Or add GwtEnt to add all modules <inherits name="com.gwtent.GwtEnt" /> To make validation message worked or support I18N to your messages, you need to add the codeline to your EntryPoint method GWTValidateMessageStore.getInstance().addMessageObject(GWT.create(ValidateMessages.class), ValidateMessages.class); or implement your own ValidationMessages. Create you domain object for validation
For example: @Reflectable
public static class ClassToValidate{
@Required
@Size(min=2, max=30)
private String firstName;
@Required
private String lastName;
//@Regular(regex="^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+", message="Please input right email address.")
//Direct using Regular of Email annotation
@Email
private String email;
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
}Validate it void onClick(ClickEvent e) {
//Create domain object and setup the values
ClassToValidate obj = new ClassToValidate();
obj.setFirstName("First Name");
obj.setLastName(null); //
obj.setEmail("not a good email address");
//Validate it by GWT validator
Set<ConstraintViolation<ClassToValidate>> validateResult = ValidatorFactory.getGWTValidator().validate(obj);
//Display the result
String errorMsg = "";
for (ConstraintViolation<ClassToValidate> violation : validateResult){
errorMsg += violation.getPropertyPath() + ":" + violation.getMessage() + " Value: " + violation.getInvalidValue() + "\n";
}
Window.alert(errorMsg);
}The resultsemail not right because it doesn't not follow the rules of a emal lastName get error because it's "Required". email:Email required. Value: not a good email address lastName:This field is required. Value: null More InfoSee the demo source file in SVN: http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent_showcase/src/main/java//com/gwtent/showcase/client/validate/ValidateByCode.java GWTENT ShowCase: http://gwtent-showcase.appspot.com/ Builtin Constraints
For developer who want implement JSR303Please send us a email, we can discuss how to build a way to share JSR303 emulation. For simple, in Validation class, I just direct created GWT validation implement. | |
► Sign in to add a comment
I think this doc is missing a major information. For Validation to work you need to add the codeline "GWTValidateMessageStore.getInstance().addMessageObject(GWT.create(ValidateMessages?.class), ValidateMessages?.class);" to your EntryPoint? method, or implement your own ValidationMessages?.
Thanks Nico, document updated.