public Validator phonenumberValidator()
{
return new Validator(){
private final String PATTERN ="/(?[0-9]{3}/)?[-. ]?[0-9]{3}[-. ]?
[0-9]{4}";
public String getErrorMessage() {
// TODO Auto-generated method stub
return "Invalid PhoneNumber";
}
public boolean isValid(String input) {
// TODO Auto-generated method stub
return PATTERN.matches(input);
}
};
}
}
public interface Validator {
abstract boolean isValid(String input);
abstract String getErrorMessage();
}
public interface ValidationListener extends EventListener {
public void onError( TextBoxBase source,String error);
public void onValidation( TextBoxBase source);
}
interface ValidationEventSource {
public abstract void addValidationListener(ValidationListener
listener_);
public abstract void removeValidationListener(ValidationListener
listener_);
}
public interface TextBoxBase {
public abstract void setError(String error_);
public abstract void setMaxLength(int maxLength);
public abstract void setMinLength(int minLength);
public abstract void setRequired(boolean required);
public abstract void validate();
public abstract void addValidator(Validator validator_);
public abstract void addValidationListener(ValidationListener
listener_);
public abstract void removeValidationListener(ValidationListener
listener_);
}
import com.google.gwt.user.client.ui.Label;
public class TextBox extends com.google.gwt.user.client.ui.TextBox
implements
TextBoxBase {
public void addValidator(Validator validator_) {
delegate.addValidator(validator_);
}
private TextBoxDelegate delegate;
private TextBox(){};
public TextBox(Label label_)
{
super();
delegate = new TextBoxDelegate(label_,this);
}
public void addValidationListener(ValidationListener listener_) {
delegate.addValidationListener(listener_);
}
public void removeValidationListener(ValidationListener listener_) {
delegate.removeValidationListener(listener_);
}
public void setError(String error_) {
delegate.setError(error_);
}
public void setMinLength(int minLength) {
delegate.setMinLength(minLength);
}
public void setRequired(boolean required) {
delegate.setRequired(required);
}
public void validate() {
delegate.validate();
}
}
import com.google.gwt.user.client.ui.Label;
public class PasswordTextBox extends
com.google.gwt.user.client.ui.PasswordTextBox implements TextBoxBase {
private TextBoxDelegate delegate;
private PasswordTextBox(){};
public PasswordTextBox(Label label_)
{
super();
delegate = new TextBoxDelegate(label_,this);
}
public void addValidationListener(ValidationListener listener_) {
delegate.addValidationListener(listener_);
}
public void removeValidationListener(ValidationListener listener_) {
delegate.removeValidationListener(listener_);
}
public void setError(String error_) {
delegate.setError(error_);
}
public void setMinLength(int minLength) {
delegate.setMinLength(minLength);
}
public void setRequired(boolean required) {
delegate.setRequired(required);
}
public void validate() {
delegate.validate();
}
public void setMaxLength(int maxLength) {
delegate.setMaxLength(maxLength);
}
public void addValidator(Validator validator_) {
delegate.addValidator(validator_);
}
}
import java.util.Iterator;
import java.util.Vector;
import com.bbh.autoservices.client.Errors;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sun.org.apache.xpath.internal.functions.Function;
class TextBoxDelegate implements
ChangeListener,FocusListener,ValidationEventSource, TextBoxBase{
private int minLength =-1;
private int maxLength =-1;
private boolean required =false;
private Label label;
private String error =null;
private com.google.gwt.user.client.ui.TextBoxBase superFake;
private TextBoxDelegate(){};
private Vector validators = new Vector();
public void addValidator(Validator validator_) {
validators.add(validator_);
}
private ValidatorImpl validator = new ValidatorImpl();
TextBoxDelegate(Label label_,
com.google.gwt.user.client.ui.TextBoxBase superFake_)
{
superFake =superFake_;
superFake.addChangeListener(this);
superFake.addFocusListener(this);
label= label_;
}
public void setError(String error_) {
this.error = error_;
Errors.getInstance().add(this, error);
label.addStyleName(Errors.STYLE);
}
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 onChange(Widget sender) {
if(error != null)
{
error=null;
Errors.getInstance().remove(this);
label.removeStyleName(Errors.STYLE);
}
}
public void onFocus(Widget sender) {
// TODO Auto-generated method stub
}
public void onLostFocus(Widget sender) {
validate();
}
public void validate()
{
if(this.required && superFake.getText() == null)
this.setError(ErrorMessages.fieldRequired(label.getText()));
else if(minLength != -1 && superFake.getText().length() <minLength)
this.setError(ErrorMessages.fieldMinLength(label.getText(),minLength));
else if(maxLength != -1 && superFake.getText().length()
>maxLength)
this.setError(ErrorMessages.fieldMaxLength(label.getText(),maxLength));
else if (isValid())
validator.raiseValidationEvent(this);
}
private boolean isValid()
{
for (Iterator iter = validators.iterator(); iter.hasNext();) {
Validator element = (Validator) iter.next();
if(!element.isValid(superFake.getText()))
{
this.setError(element.getErrorMessage());
return false;
}
}
return true;
}
private static final class ErrorMessages {
private ErrorMessages(){};
public static final String fieldRequired(String field)
{
return field + " is required.";
}
public static final String fieldMinLength(String field, int
minLength)
{
return field + " can not be less than " + minLength + "
characters.";
}
public static String fieldMaxLength(String field, int maxLength)
{
return field + " can not be more than " + maxLength + "
characters.";
}
}
public void addValidationListener(ValidationListener listener_) {
validator.addValidationListener(listener_);
}
public void removeValidationListener(ValidationListener listener_) {
validator.removeValidationListener(listener_);
}
private class ValidatorImpl implements ValidationEventSource {
private Vector listeners = new Vector();
public void addValidationListener(ValidationListener listener_)
{
listeners.add(listener_);
}
public void removeValidationListener(ValidationListener listener_)
{
listeners.remove(listener_);
}
public void raiseError(TextBoxBase source, String error)
{
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
ValidationListener element = (ValidationListener) iter.next();
element.onError(source, error);
}
}
public void raiseValidationEvent(TextBoxBase source_)
{
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
ValidationListener element = (ValidationListener) iter.next();
element.onValidation(source_);
}
}
}
}
public interface Validator {
abstract boolean isInputValid(String input);
abstract String getErrorMessage();
}
public class ValidatorFactory {
public final static Validator phonenumberValidator()
{
return new Validator(){
private final String PATTERN ="\\(?[0-9]{3}\\)?[-. ]?[0-9]{3}[-. ]?
[0-9]{4}";
public String getErrorMessage() {
// TODO Auto-generated method stub
return "Invalid PhoneNumber";
}
public boolean isInputValid(String input) {
package com.bbh.autoservices.client.ui;
import com.google.gwt.user.client.ui.Label;
public class PasswordTextBox extends
com.google.gwt.user.client.ui.PasswordTextBox implements TextBoxBase {
public boolean hasError() {
return delegate.hasError();
}
private TextBoxDelegate delegate;
private PasswordTextBox(){};
public PasswordTextBox(Label label_)
{
super();
delegate = new TextBoxDelegate(label_,this);
}
public void addValidationListener(ValidationListener listener_) {
delegate.addValidationListener(listener_);
}
public void removeValidationListener(ValidationListener listener_) {
delegate.removeValidationListener(listener_);
}
public void setError(String error_) {
delegate.setError(error_);
}
public void setRequired(boolean required) {
delegate.setRequired(required);
}
public void validate() {
delegate.validate();
}
public void addValidator(Validator validator_) {
delegate.addValidator(validator_);
}
}
package com.bbh.autoservices.client.ui;
import com.google.gwt.user.client.ui.Label;
public class TextBox extends com.google.gwt.user.client.ui.TextBox
implements
TextBoxBase {
public void addValidator(Validator validator_) {
delegate.addValidator(validator_);
}
private TextBoxDelegate delegate;
private TextBox(){};
public TextBox(Label label_)
{
super();
delegate = new TextBoxDelegate(label_,this);
}
public void addValidationListener(ValidationListener listener_) {
delegate.addValidationListener(listener_);
}
public void removeValidationListener(ValidationListener listener_) {
delegate.removeValidationListener(listener_);
}
public void setError(String error_) {
delegate.setError(error_);
}
public void setRequired(boolean required) {
delegate.setRequired(required);
}
public void validate() {
delegate.validate();
}
public boolean hasError() {
return delegate.hasError();
}
}
package com.bbh.autoservices.client.ui;
public interface TextBoxBase {
public abstract void setError(String error_);
public abstract void setRequired(boolean required);
public abstract void validate();
public abstract void addValidator(Validator validator_);
public abstract void addValidationListener(ValidationListener
listener_);
public abstract void removeValidationListener(ValidationListener
listener_);
public abstract boolean hasError();
}
package com.bbh.autoservices.client.ui;
import java.util.Iterator;
import java.util.Vector;
import com.bbh.autoservices.client.Errors;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
class TextBoxDelegate implements
ChangeListener,FocusListener,ValidationEventSource, TextBoxBase{
public boolean hasError() {
// TODO Auto-generated method stub
return (this.error != null);
}
private boolean required =false;
private Label label;
private String error =null;
private com.google.gwt.user.client.ui.TextBoxBase superFake;
private TextBoxDelegate(){};
private Vector validators = new Vector();
public void addValidator(Validator validator_) {
validators.add(validator_);
}
private ValidatorImpl validator = new ValidatorImpl();
TextBoxDelegate(Label label_,
com.google.gwt.user.client.ui.TextBoxBase superFake_)
{
superFake =superFake_;
superFake.addChangeListener(this);
superFake.addFocusListener(this);
label= label_;
label.addStyleName("as-TextBoxLabel");
}
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.TextBoxBase#setError(java.lang.String)
*/
public void setError(String error_) {
this.error = error_;
Errors.getInstance().add(this, error);
label.addStyleName(Errors.STYLE);
}
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.TextBoxBase#setRequired(boolean)
*/
public void setRequired(boolean required_) {
if(required_ != this.required)
{
if(required_)
label.addStyleName("as-RequiredTextBoxLabel");
else
label.removeStyleName("as-RequiredTextBoxLabel");
}
this.required = required_;
}
public void onChange(Widget sender) {
if(error != null)
{
error=null;
Errors.getInstance().remove(this);
label.removeStyleName(Errors.STYLE);
}
}
public void onFocus(Widget sender) {
// TODO Auto-generated method stub
}
public void onLostFocus(Widget sender) {
validate();
}
/* (non-Javadoc)
* @see com.bbh.autoservices.client.ui.TextBoxBase#validate()
*/
public void validate()
{
if(this.required && superFake.getText() == null)
this.setError(label.getText() + " is required.");
else if(superFake.getText() == null);
else if (isValid())
validator.raiseValidationEvent(this);
}
private boolean isValid()
{
for (Iterator iter = validators.iterator(); iter.hasNext();) {
Validator element = (Validator) iter.next();
if(!element.isInputValid(superFake.getText()))
{
this.setError(element.getErrorMessage());
return false;
}
}
return true;
}
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.TextBoxBase#addValidationListener(com.bbh.autoservices.client.ui.ValidationListener)
*/
public void addValidationListener(ValidationListener listener_) {
validator.addValidationListener(listener_);
}
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.TextBoxBase#removeValidationListener(com.bbh.autoservices.client.ui.ValidationListener)
*/
public void removeValidationListener(ValidationListener listener_) {
validator.removeValidationListener(listener_);
}
private class ValidatorImpl implements ValidationEventSource {
private Vector listeners = new Vector();
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.Validator#addValidationListener(com.bbh.autoservices.client.ValidationListener)
*/
public void addValidationListener(ValidationListener listener_)
{
listeners.add(listener_);
}
/* (non-Javadoc)
* @see
com.bbh.autoservices.client.ui.Validator#removeValidationListener(com.bbh.autoservices.client.ValidationListener)
*/
public void removeValidationListener(ValidationListener listener_)
{
listeners.remove(listener_);
}
public void raiseError(TextBoxBase source, String error)
{
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
ValidationListener element = (ValidationListener) iter.next();
element.onError(source, error);
}
}
public void raiseValidationEvent(TextBoxBase source_)
{
for (Iterator iter = listeners.iterator(); iter.hasNext();) {
ValidationListener element = (ValidationListener) iter.next();
element.onValidation(source_);
}
}
}
}
package com.bbh.autoservices.client.ui;
interface ValidationEventSource {
public abstract void addValidationListener(ValidationListener
listener_);
public abstract void removeValidationListener(ValidationListener
listener_);
}
package com.bbh.autoservices.client.ui;
import java.util.EventListener;
public interface ValidationListener extends EventListener {
public void onErrorReSet( TextBoxBase source,String error);
public void onError( TextBoxBase source,String error);
public void onValidation( TextBoxBase source);
}
package com.bbh.autoservices.client.ui;
public interface Validator {
abstract boolean isInputValid(String input);
abstract String getErrorMessage();
}
package com.bbh.autoservices.client.ui;
public class ValidatorFactory {
public final static Validator createPhonenumberValidator()
{
return new Validator(){
private final String PATTERN ="\\(?[0-9]{3}\\)?[-. ]?[0-9]{3}[-. ]?
[0-9]{4}";
public String getErrorMessage() {
// TODO Auto-generated method stub
return "Invalid PhoneNumber";
}
public boolean isInputValid(String input) {
// TODO Auto-generated method stub
return PATTERN.matches(input);
}
};
}
public final static Validator createEmailValidator()
{
return new Validator(){
private final String PATTERN ="^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]
{2,4}$";
public String getErrorMessage() {
// TODO Auto-generated method stub
return "Invalid e-mail address.";
}
public boolean isInputValid(String input) {
// TODO Auto-generated method stub
return PATTERN.matches(input);
}
};
}
public final static Validator createMinLengthValidator(int
minLength_, String fieldName_)
{
final int minLength =minLength_;
final String fieldName =fieldName_;
return new Validator( ){
public String getErrorMessage() {
return fieldName + " can not be less than " + minLength + "
characters.";
}
public boolean isInputValid(String input) {
return (input.length() >= minLength);
}
};
}
public final static Validator createMaxLengthValidator(int
maxLength_, String fieldName_)
{
final int maxLength =maxLength_;
final String fieldName =fieldName_;
return new Validator( ){
public String getErrorMessage() {
return fieldName + " can not be more than " + maxLength + "
characters.";
}
public boolean isInputValid(String input) {
return (input.length() <= maxLength);
}
};
}
}
package com.bbh.autoservices.client;
import java.util.HashMap;
import java.util.Iterator;
import com.bbh.autoservices.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
public final class Errors {
public static final String STYLE="as-Error" ;
private static Errors singleton = new Errors();
private ErrorPanel errorPanel =new ErrorPanel() ;
private ErrorMessages messages = new ErrorMessages();
private HashMap errors = new HashMap();
private Errors(){};
public void add(TextBoxBase source_, String error_)
{
errors.put(source_, error_);
errorPanel.update();
}
public static final Errors getInstance()
{
return singleton;
}
public ErrorPanel getErrorPanel()
{
return errorPanel;
}
public ErrorMessages getErrorMessages()
{
return messages;
}
public void remove(TextBoxBase source_)
{
errors.remove(source_);
errorPanel.update();
}
public final class ErrorPanel extends Composite {
private Label error = new Label();
private ErrorPanel()
{
VerticalPanel container =new VerticalPanel();
container.add(error);
error.addStyleName(Errors.STYLE);
initWidget(container);
}
private void update()
{
error.setText("");
error.setVisible(false);
for (Iterator iter = errors.entrySet().iterator(); iter.hasNext();)
{
String element = (String) iter.next();
error.setVisible(true);
error.setText(element);
}
}
}
public final class ErrorMessages {
}
}
and here is code snippet that uses this frame work
import com.bbh.autoservices.client.ui.TextBox;
import com.bbh.autoservices.client.ui.TextBoxBase;
import com.bbh.autoservices.client.ui.ValidationListener;
import com.bbh.autoservices.client.ui.ValidatorFactory;
private Label emailLbl = new Label("Email Address");
private TextBox email = new TextBox(emailLbl);
email.setRequired(true);
email.addValidator(ValidatorFactory.createEmailValidator());
Thank you agata,
Here is how it works (assuming you have used some GWT :) )
public final class RegistrationForm extends Composite implements ValidationListener {
public void onErrorReSet(TextBoxBase source, String error) {
}
public void onError(TextBoxBase source, String error) {
// TODO Auto-generated method stub
}
public void onValidation(TextBoxBase source) {
if(source == confirmPassword)
{
if(!confirmPassword.getText().equals(password.getText()))
confirmPassword.setError(confirmPasswordLbl.getText() + " must equal " + passwordLbl.getText());
}
}
private Label usernameLbl = new Label("Username");
private Label passwordLbl = new Label("Password");
private Label confirmPasswordLbl = new Label("Confirm Password");
private TextBox username= new TextBox(usernameLbl);
private PasswordTextBox password= new PasswordTextBox(passwordLbl);
private PasswordTextBox confirmPassword= new PasswordTextBox(confirmPasswordLbl);
public RegistrationForm() {
VerticalPanel outer =new VerticalPanel();
HorizontalPanel inner = new HorizontalPanel();
VerticalPanel left = new VerticalPanel();
VerticalPanel right = new VerticalPanel();
left.add(usernameLbl);
left.add(passwordLbl);
left.add(confirmPasswordLbl);
username.setRequired(true);
username.addValidator(ValidatorFactory.createMinLengthValidator (3, usernameLbl.getText()));
username.addValidator(ValidatorFactory.createMaxLengthValidator(20, usernameLbl.getText()));
password.setRequired(true);
password.addValidator (ValidatorFactory.createMinLengthValidator(6, password.getText()));
password.addValidator(ValidatorFactory.createMaxLengthValidator(20, password.getText()));
confirmPassword.setRequired(true);
right.add(username);
right.add(password);
right.add(confirmPassword);
inner.add(left);
inner.add(right);
outer.add(inner);
initWidget(outer);
}
public PasswordTextBox getConfirmPassword() {
return confirmPassword;
}
public PasswordTextBox getPassword() {
return password;
}
public TextBox getUsername() {
return username;
}
}
On Nov 30, 2007 5:41 AM, agata...@gmail.com <agata...@gmail.com > wrote:Hello..
Can you insert here more specific code how to use this validator.
I mean code that chekcs validity and shows some error messages .
Btw nice work;)
> this.setError (label.getText() + " is required.");
> for (Iterator iter = listeners.iterator (); iter.hasNext();) {