|
For any component, you can add a client side validation by implementing com.creamtec.ajaxswing.support.validation.Validatable
Interface or one of it’s descendent.
How AjaxSwing Supports Client Side Validation
AjaxSwing adds Javascript listeners, which get activated when the focus is lost from the HTML input and if the entered data is invalid, AjaxSwing will displays popup window, which shows error message.
AjaxSwing Client Side Validation Types
AjaxSwing supports a lot of client side validations through this interface or
any one of it’s descendent. For example you can check user’s input
against specific mask, required, range, …. etc, also you can specify
custom error message for user or use default error message.
Current version of AjaxSwing has the following validation interfaces :
com.creamtec.ajaxswing.support.validation.Validatable
: Holds common validation
methods.
com.creamtec.ajaxswing.support.validation.ValidatableDate : Extends com.creamtec.ajaxswing.support.validation.Validatable and add extra validation
methods specific for date.
com.creamtec.ajaxswing.support.validation.ValidatableFloat : Extends com.creamtec.ajaxswing.support.validation.Validatable and add extra validation
methods specific for float.
com.creamtec.ajaxswing.support.validation.ValidatableInt : Extends com.creamtec.ajaxswing.support.validation.Validatable and add extra validation
methods specific for integer.
Example Implementation of Validtable Interface
In this example we will create a new class called JTextFieldValidatable
which exetends javax.swing.JTextField and implement com.creamtec.ajaxswing.support.validation.Validatable
Interface.
package com.creamtec.ajaxswing.ui;
import javax.swing.JTextField;
import com.creamtec.ajaxswing.support.validation.Validatable;
public class JTextFieldValidatable extends JTextField implements Validatable {
private String displayName;
private String errorMessage;
private String mask = Validatable.DEFUALT_MASK;
private boolean required = Validatable.DEFUALT_REQUIRED;
private int maxLength = Validatable.DEFUALT_MAX_LENGTH;
private int minLength = Validatable.DEFUALT_MIN_LENGTH;
public String getMask() {
return mask;
}
public void setMask(String mask) {
this.mask = mask;
}
public int getMaxLength() {
return maxLength;
}
public int getMinLength() {
return minLength;
}
public boolean isRequired() {
return required;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getErrorMessage() {
return errorMessage;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public void setRequired(boolean required) {
this.required = required;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
In
our implementation of JTextFieldValidatable we initialize all
validations with default values, AjaxSwing will add validation to the component
at rendering time only if its value is not equal to the corresponding default
value. By this way you can disable specific validation by setting its value to
the default.
In
our code when we create instance of JTextFieldValidatable class,
we will add our validation to the created instance :
JTextFieldValidatable
txfEmail = new JTextFieldValidatable();
Add required validation :
txfEmail.setRequired(true);
Add email validation using predefined mask, AjaxSwing has a lot of predefined masks ( email, telephone, credit card …etc)
txfEmail.setMask(Validatable.MASK_EMAIL);
If you want to use specific mask you can pass this mask to setMask method like the above example.
Add Custom Error Message :
txfEmail.setErrorMessage(“User Email is required and must be valid mail”);
If you want to use AjaxSwing default error message instead of using custom error message, then you have to set Display Name, which will display in the default error message :
txfEmail.setDisplayName(“User Email”);
|