Add Client Side Validation
For any component, you can add a client side validation by implementing creamtec.webcream.validation.Validatable
Interface or one of it’s descendent.
How WebCream Supports Client Side Validation
WebCream adds Javascript listeners, which get activated when the focus is lost from the HTML input and if the entered data is invalid, WebCream will displays popup window, which shows error message.
WebCream Client Side Validation Types
WebCream 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 WebCream has the following validation interfaces :
creamtec.webcream.validation.Validatable
:
Holds common validation
methods.creamtec.webcream.validation.ValidatableDate
:
Extends creamtec.webcream.validation.Validatable
and add extra validation
methods specific for date.creamtec.webcream.validation.ValidatableFloat
:
Extends creamtec.webcream.validation.Validatable
and add extra validation
methods specific for float.creamtec.webcream.validation.ValidatableInt
:
Extends creamtec.webcream.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 creamtec.webcream.validation.Validatable
Interface.
package creamtec.webcream.ui; import javax.swing.JTextField; import creamtec.webcream.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, WebCream 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, WebCream 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 WebCream 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”);