I'm new to GWT. What good solutions exist开发者_运维百科 for form validation? I'd like to avoid rolling my own if possible.
In my GWT applications I always use my custom validator classes. I have created my own textbox class which extends gwt textbox. And I call CustomTextBox instead of gwt's textbox.
CustomTextBox.java
public class CustomTextBox extends TextBox implements HasValidators{
private static final String TEXTBOX_VALIDATION_ERROR_STYLE = "error-text-box";
private String errorMessage = "";
private List<Validator> validators = new ArrayList<Validator>();
public CustomTextBox() {
}
public CustomTextBox(String name) {
setName(name);
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public void addValidator(Validator validator) {
validators.add(validator);
}
public boolean validate() {
boolean validationResult = true;
for (Validator validator : validators) {
validationResult = validator.validate(getValue().trim());
if (!validationResult) {
errorMessage = validator.getErrorMessage();
break;
}
errorMessage = validator.getErrorMessage();
}
setErrorStyles(validationResult);
return validationResult;
}
private void setErrorStyles(boolean validationResult) {
if (validationResult) {
removeStyleName(TEXTBOX_VALIDATION_ERROR_STYLE);
setTitle("");
} else {
addStyleName(TEXTBOX_VALIDATION_ERROR_STYLE);
setTitle(errorMessage);
}
}
@Override
public void setValue(String s) {
removeStyleDependentName(TEXTBOX_VALIDATION_ERROR_STYLE);
super.setValue(s);
}
@Override
public String getValue() {
return super.getValue().trim();
}
}
Validator.java
public abstract class Validator {
public String errorMessage;
public abstract boolean validate(String value);
public abstract String getErrorMessage();
}
Sample Email validator
public class EmailValidator extends Validator {
public boolean validate(String value) {
if (value.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")) {
errorMessage = "";
return true;
} else {
errorMessage = "Enter valid email Id";
return false;
}
}
public String getErrorMessage() {
return errorMessage;
}
}
My validation error looks like following
If you like this approach you can follow this. The problem here is we don't display the error message directly in the UI. Only in tooltip we are showing.
GXT and SmartGWT (both of them I'd highly advise against, for many reasons) have form validation.
GWT's Editor framework provides a mean to display javax.validation.ConstraintViolation
(you'd still have to do the actual display by implementing HasEditorErrors
to receive the errors for a given field; the Editor framework only dispatches the errors to the appropriate "fields") but nothing to validate on the client side.
The recently released, GWT 2.3 has preliminary support for JSR 303 Bean Validation on the client side but it's not yet finished: http://code.google.com/p/google-web-toolkit/wiki/BeanValidation I think GWT 2.4 will have full (or almost full) support.
Note that GWT's stake on validation is on validating the objects, not about validating the "form fields" editing an object's properties.
We are currently using this project for forms validation: http://code.google.com/p/gwt-validation/
It's the source code that serves as the basis of the new GWT 2.4 Validation framework.
Chris Buffalo has been doing an amazing work on that project. Works out of the box for us.
精彩评论