summaryrefslogtreecommitdiffstats
path: root/src/com/itmill
diff options
context:
space:
mode:
authorHenri Sara <henri.sara@itmill.com>2009-04-03 07:15:26 +0000
committerHenri Sara <henri.sara@itmill.com>2009-04-03 07:15:26 +0000
commitf7dcedfd372eb05d68feec7bc0dc7a3e0a59d721 (patch)
treee51a165ee3efa2fca1a61be2848cbe20ba6e46a1 /src/com/itmill
parent651ab66ebf222a1220db7f05f39f04ab19bf7314 (diff)
downloadvaadin-framework-f7dcedfd372eb05d68feec7bc0dc7a3e0a59d721.tar.gz
vaadin-framework-f7dcedfd372eb05d68feec7bc0dc7a3e0a59d721.zip
#680: merged to 6.0: validator base classes and standard validators for regular expressions, numbers and e-mail addresses; test class for validators
svn changeset:7292/svn branch:6.0
Diffstat (limited to 'src/com/itmill')
-rw-r--r--src/com/itmill/toolkit/data/validator/AbstractStringValidator.java54
-rw-r--r--src/com/itmill/toolkit/data/validator/AbstractValidator.java67
-rw-r--r--src/com/itmill/toolkit/data/validator/CompositeValidator.java58
-rw-r--r--src/com/itmill/toolkit/data/validator/DoubleValidator.java36
-rw-r--r--src/com/itmill/toolkit/data/validator/EmailValidator.java31
-rw-r--r--src/com/itmill/toolkit/data/validator/IntegerValidator.java37
-rw-r--r--src/com/itmill/toolkit/data/validator/RegexpValidator.java86
-rw-r--r--src/com/itmill/toolkit/data/validator/StringLengthValidator.java78
-rw-r--r--src/com/itmill/toolkit/tests/validation/TestValidators.java163
9 files changed, 509 insertions, 101 deletions
diff --git a/src/com/itmill/toolkit/data/validator/AbstractStringValidator.java b/src/com/itmill/toolkit/data/validator/AbstractStringValidator.java
new file mode 100644
index 0000000000..7f26a87861
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/AbstractStringValidator.java
@@ -0,0 +1,54 @@
+package com.itmill.toolkit.data.validator;
+
+/**
+ * Validator base class for validating strings. See
+ * {@link com.itmill.toolkit.data.validator.AbstractValidator} for more
+ * information.
+ *
+ * <p>
+ * If the validation fails, the exception thrown contains the error message with
+ * its argument 0 replaced with the string being validated.
+ * </p>
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public abstract class AbstractStringValidator extends AbstractValidator {
+
+ /**
+ * Constructs a validator for strings.
+ * <p>
+ * Null and empty string values are always accepted. To disallow empty
+ * values, set the field being validated as required.
+ * </p>
+ *
+ * @param errorMessage
+ * the message included in the exception (with its parameter {0}
+ * replaced by the string to be validated) in case the validation
+ * fails
+ */
+ public AbstractStringValidator(String errorMessage) {
+ super(errorMessage);
+ }
+
+ public boolean isValid(Object value) {
+ if (value == null) {
+ return true;
+ }
+ if (!(value instanceof String)) {
+ return false;
+ }
+ return isValidString((String) value);
+ }
+
+ /**
+ * Checks if the given string is valid.
+ *
+ * @param value
+ * String to check. Can never be null.
+ * @return true if the string is valid, false otherwise
+ */
+ protected abstract boolean isValidString(String value);
+}
diff --git a/src/com/itmill/toolkit/data/validator/AbstractValidator.java b/src/com/itmill/toolkit/data/validator/AbstractValidator.java
new file mode 100644
index 0000000000..93d6ae41fa
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/AbstractValidator.java
@@ -0,0 +1,67 @@
+package com.itmill.toolkit.data.validator;
+
+import com.itmill.toolkit.data.Validator;
+
+/**
+ * Default Validator base class. See
+ * {@link com.itmill.toolkit.data.validator.Validator} for more information.
+ * <p>
+ * If the validation fails, the exception thrown contains the error message with
+ * its argument 0 replaced with the toString() of the object being validated.
+ * </p>
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public abstract class AbstractValidator implements Validator {
+
+ /**
+ * Error message.
+ */
+ private String errorMessage;
+
+ /**
+ * Constructs a validator with an error message.
+ *
+ * @param errorMessage
+ * the message included in the exception (with its parameter {0}
+ * replaced by toString() of the object to be validated) in case
+ * the validation fails
+ */
+ public AbstractValidator(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public void validate(Object value) throws InvalidValueException {
+ if (!isValid(value)) {
+ String message;
+ if (value == null) {
+ message = errorMessage.replace("{0}", "null");
+ } else {
+ message = errorMessage.replace("{0}", value.toString());
+ }
+ throw new InvalidValueException(message);
+ }
+ }
+
+ /**
+ * Gets the message to be displayed in case the value does not validate.
+ *
+ * @return the Error Message.
+ */
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ /**
+ * Sets the message to be displayed in case the value does not validate.
+ *
+ * @param errorMessage
+ * the Error Message to set.
+ */
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+}
diff --git a/src/com/itmill/toolkit/data/validator/CompositeValidator.java b/src/com/itmill/toolkit/data/validator/CompositeValidator.java
index f8ac2fc7eb..6aa9d07b99 100644
--- a/src/com/itmill/toolkit/data/validator/CompositeValidator.java
+++ b/src/com/itmill/toolkit/data/validator/CompositeValidator.java
@@ -6,8 +6,8 @@ package com.itmill.toolkit.data.validator;
import java.util.Collection;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.LinkedList;
+import java.util.List;
import com.itmill.toolkit.data.Validator;
@@ -23,7 +23,7 @@ import com.itmill.toolkit.data.Validator;
* @VERSION@
* @since 3.0
*/
-public class CompositeValidator implements Validator {
+public class CompositeValidator extends AbstractValidator {
/**
* The validators are combined with <code>AND</code> clause: validity of the
@@ -53,26 +53,22 @@ public class CompositeValidator implements Validator {
/**
* List of contained validators.
*/
- private final LinkedList validators = new LinkedList();
-
- /**
- * Error message.
- */
- private String errorMessage;
+ private final List<Validator> validators = new LinkedList<Validator>();
/**
* Construct a composite validator in <code>AND</code> mode without error
* message.
*/
public CompositeValidator() {
+ super("");
}
/**
* Constructs a composite validator in given mode.
*/
public CompositeValidator(int mode, String errorMessage) {
+ super(errorMessage);
setMode(mode);
- setErrorMessage(errorMessage);
}
/**
@@ -94,19 +90,20 @@ public class CompositeValidator implements Validator {
* @throws Validator.InvalidValueException
* if the value is not valid.
*/
+ @Override
public void validate(Object value) throws Validator.InvalidValueException {
switch (mode) {
case MODE_AND:
- for (final Iterator i = validators.iterator(); i.hasNext();) {
- ((Validator) i.next()).validate(value);
+ for (Validator validator : validators) {
+ validator.validate(value);
}
return;
case MODE_OR:
Validator.InvalidValueException first = null;
- for (final Iterator i = validators.iterator(); i.hasNext();) {
+ for (Validator v : validators) {
try {
- ((Validator) i.next()).validate(value);
+ v.validate(value);
return;
} catch (final Validator.InvalidValueException e) {
if (first == null) {
@@ -141,8 +138,7 @@ public class CompositeValidator implements Validator {
public boolean isValid(Object value) {
switch (mode) {
case MODE_AND:
- for (final Iterator i = validators.iterator(); i.hasNext();) {
- final Validator v = (Validator) i.next();
+ for (Validator v : validators) {
if (!v.isValid(value)) {
return false;
}
@@ -150,8 +146,7 @@ public class CompositeValidator implements Validator {
return true;
case MODE_OR:
- for (final Iterator i = validators.iterator(); i.hasNext();) {
- final Validator v = (Validator) i.next();
+ for (Validator v : validators) {
if (v.isValid(value)) {
return true;
}
@@ -193,9 +188,10 @@ public class CompositeValidator implements Validator {
* Gets the error message for the composite validator. If the error message
* is null, original error messages of the sub-validators are used instead.
*/
+ @Override
public String getErrorMessage() {
- if (errorMessage != null) {
- return errorMessage;
+ if (getErrorMessage() != null) {
+ return getErrorMessage();
}
// TODO Return composite error message
@@ -204,17 +200,6 @@ public class CompositeValidator implements Validator {
}
/**
- * Sets the error message for the composite validator. If the error message
- * is null, original error messages of the sub-validators are used instead.
- *
- * @param errorMessage
- * the Error Message to set.
- */
- public void setErrorMessage(String errorMessage) {
- this.errorMessage = errorMessage;
- }
-
- /**
* Adds validator to the interface.
*
* @param validator
@@ -254,23 +239,22 @@ public class CompositeValidator implements Validator {
* validators of given type null is returned.
* </p>
*
- * @return Collection of validators compatible with given type that must
- * apply or null if none fould.
+ * @return Collection<Validator> of validators compatible with given type
+ * that must apply or null if none fould.
*/
- public Collection getSubValidators(Class validatorType) {
+ public Collection<Validator> getSubValidators(Class validatorType) {
if (mode != MODE_AND) {
return null;
}
- final HashSet found = new HashSet();
- for (final Iterator i = validators.iterator(); i.hasNext();) {
- final Validator v = (Validator) i.next();
+ final HashSet<Validator> found = new HashSet<Validator>();
+ for (Validator v : validators) {
if (validatorType.isAssignableFrom(v.getClass())) {
found.add(v);
}
if (v instanceof CompositeValidator
&& ((CompositeValidator) v).getMode() == MODE_AND) {
- final Collection c = ((CompositeValidator) v)
+ final Collection<Validator> c = ((CompositeValidator) v)
.getSubValidators(validatorType);
if (c != null) {
found.addAll(c);
diff --git a/src/com/itmill/toolkit/data/validator/DoubleValidator.java b/src/com/itmill/toolkit/data/validator/DoubleValidator.java
new file mode 100644
index 0000000000..56f537f5e4
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/DoubleValidator.java
@@ -0,0 +1,36 @@
+package com.itmill.toolkit.data.validator;
+
+/**
+ * String validator for a double precision floating point number. See
+ * {@link com.itmill.toolkit.data.validator.AbstractStringValidator} for more
+ * information.
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public class DoubleValidator extends AbstractStringValidator {
+
+ /**
+ * Creates a validator for checking that a string can be parsed as an
+ * double.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public DoubleValidator(String errorMessage) {
+ super(errorMessage);
+ }
+
+ @Override
+ protected boolean isValidString(String value) {
+ try {
+ Double.parseDouble(value);
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+}
diff --git a/src/com/itmill/toolkit/data/validator/EmailValidator.java b/src/com/itmill/toolkit/data/validator/EmailValidator.java
new file mode 100644
index 0000000000..b5abb4d712
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/EmailValidator.java
@@ -0,0 +1,31 @@
+package com.itmill.toolkit.data.validator;
+
+/**
+ * String validator for e-mail addresses. The e-mail address syntax is not
+ * complete according to RFC 822 but handles the vast majority of valid e-mail
+ * addresses correctly.
+ *
+ * See {@link com.itmill.toolkit.data.validator.AbstractStringValidator} for
+ * more information.
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public class EmailValidator extends RegexpValidator {
+
+ /**
+ * Creates a validator for checking that a string is a syntactically valid
+ * e-mail address.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public EmailValidator(String errorMessage) {
+ super(
+ "^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$",
+ true, errorMessage);
+ }
+
+}
diff --git a/src/com/itmill/toolkit/data/validator/IntegerValidator.java b/src/com/itmill/toolkit/data/validator/IntegerValidator.java
new file mode 100644
index 0000000000..f48e85943e
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/IntegerValidator.java
@@ -0,0 +1,37 @@
+package com.itmill.toolkit.data.validator;
+
+/**
+ * String validator for integers. See
+ * {@link com.itmill.toolkit.data.validator.AbstractStringValidator} for more
+ * information.
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public class IntegerValidator extends AbstractStringValidator {
+
+ /**
+ * Creates a validator for checking that a string can be parsed as an
+ * integer.
+ *
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public IntegerValidator(String errorMessage) {
+ super(errorMessage);
+
+ }
+
+ @Override
+ protected boolean isValidString(String value) {
+ try {
+ Integer.parseInt(value);
+ return true;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+}
diff --git a/src/com/itmill/toolkit/data/validator/RegexpValidator.java b/src/com/itmill/toolkit/data/validator/RegexpValidator.java
new file mode 100644
index 0000000000..56316bea84
--- /dev/null
+++ b/src/com/itmill/toolkit/data/validator/RegexpValidator.java
@@ -0,0 +1,86 @@
+package com.itmill.toolkit.data.validator;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * String validator comparing the string against a Java regular expression. Both
+ * complete matches and substring matches are supported.
+ *
+ * <p>
+ * For the Java regular expression syntax, see
+ * {@link java.util.regex.Pattern#sum}
+ * </p>
+ * <p>
+ * See {@link com.itmill.toolkit.data.validator.AbstractStringValidator} for
+ * more information.
+ * </p>
+ *
+ * @author IT Mill Ltd.
+ * @version
+ * @VERSION@
+ * @since 5.4
+ */
+public class RegexpValidator extends AbstractStringValidator {
+
+ private Pattern pattern;
+ private boolean complete;
+ private Matcher matcher = null;
+
+ /**
+ * Creates a validator for checking that the regular expression matches the
+ * complete string to validate.
+ *
+ * @param regexp
+ * a Java regular expression
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public RegexpValidator(String regexp, String errorMessage) {
+ this(regexp, true, errorMessage);
+ }
+
+ /**
+ * Creates a validator for checking that the regular expression matches the
+ * string to validate.
+ *
+ * @param regexp
+ * a Java regular expression
+ * @param complete
+ * true to use check for a complete match, false to look for a
+ * matching substring
+ * @param errorMessage
+ * the message to display in case the value does not validate.
+ */
+ public RegexpValidator(String regexp, boolean complete, String errorMessage) {
+ super(errorMessage);
+ pattern = Pattern.compile(regexp);
+ this.complete = complete;
+ }
+
+ @Override
+ protected boolean isValidString(String value) {
+ if (complete) {
+ return getMatcher(value).matches();
+ } else {
+ return getMatcher(value).find();
+ }
+ }
+
+ /**
+ * Get a new or reused matcher for the pattern
+ *
+ * @param value
+ * the string to find matches in
+ * @return Matcher for the string
+ */
+ private Matcher getMatcher(String value) {
+ if (matcher == null) {
+ matcher = pattern.matcher(value);
+ } else {
+ matcher.reset(value);
+ }
+ return matcher;
+ }
+
+}
diff --git a/src/com/itmill/toolkit/data/validator/StringLengthValidator.java b/src/com/itmill/toolkit/data/validator/StringLengthValidator.java
index df702cfea4..e4c28041a7 100644
--- a/src/com/itmill/toolkit/data/validator/StringLengthValidator.java
+++ b/src/com/itmill/toolkit/data/validator/StringLengthValidator.java
@@ -4,8 +4,6 @@
package com.itmill.toolkit.data.validator;
-import com.itmill.toolkit.data.Validator;
-
/**
* This <code>StringLengthValidator</code> is used to validate the length of
* strings.
@@ -15,7 +13,7 @@ import com.itmill.toolkit.data.Validator;
* @VERSION@
* @since 3.0
*/
-public class StringLengthValidator implements Validator {
+public class StringLengthValidator extends AbstractValidator {
private int minLength = -1;
@@ -23,8 +21,6 @@ public class StringLengthValidator implements Validator {
private boolean allowNull = true;
- private String errorMessage;
-
/**
* Creates a new StringLengthValidator with a given error message.
*
@@ -32,7 +28,7 @@ public class StringLengthValidator implements Validator {
* the message to display in case the value does not validate.
*/
public StringLengthValidator(String errorMessage) {
- setErrorMessage(errorMessage);
+ super(errorMessage);
}
/**
@@ -42,11 +38,12 @@ public class StringLengthValidator implements Validator {
* @param errorMessage
* the message to display in case the value does not validate.
* @param minLength
- * the minimum permissable length of the string.
+ * the minimum permissible length of the string.
* @param maxLength
- * the maximum permissable length of the string.
+ * the maximum permissible length of the string.
* @param allowNull
- * Are null strings permissable?
+ * Are null strings permissible? This can be handled better by
+ * setting a field as required or not.
*/
public StringLengthValidator(String errorMessage, int minLength,
int maxLength, boolean allowNull) {
@@ -57,37 +54,6 @@ public class StringLengthValidator implements Validator {
}
/**
- * Validates the value.
- *
- * @param value
- * the value to validate.
- * @throws Validator.InvalidValueException
- * if the value was invalid.
- */
- public void validate(Object value) throws Validator.InvalidValueException {
- if (value == null) {
- if (allowNull) {
- return;
- } else {
- throw new Validator.InvalidValueException(errorMessage);
- }
- }
- final String s = value.toString();
- if (s == null) {
- if (allowNull) {
- return;
- } else {
- throw new Validator.InvalidValueException(errorMessage);
- }
- }
- final int len = s.length();
- if ((minLength >= 0 && len < minLength)
- || (maxLength >= 0 && len > maxLength)) {
- throw new Validator.InvalidValueException(errorMessage);
- }
- }
-
- /**
* Checks if the given value is valid.
*
* @param value
@@ -116,12 +82,13 @@ public class StringLengthValidator implements Validator {
* @return <code>true</code> if allows null string, otherwise
* <code>false</code>.
*/
+ @Deprecated
public final boolean isNullAllowed() {
return allowNull;
}
/**
- * Gets the maximum permissable length of the string.
+ * Gets the maximum permissible length of the string.
*
* @return the maximum length of the string.
*/
@@ -130,7 +97,7 @@ public class StringLengthValidator implements Validator {
}
/**
- * Gets the minimum permissable length of the string.
+ * Gets the minimum permissible length of the string.
*
* @return the minimum length of the string.
*/
@@ -139,14 +106,16 @@ public class StringLengthValidator implements Validator {
}
/**
- * Sets whether null-strings are to be allowed.
+ * Sets whether null-strings are to be allowed. This can be better handled
+ * by setting a field as required or not.
*/
+ @Deprecated
public void setNullAllowed(boolean allowNull) {
this.allowNull = allowNull;
}
/**
- * Sets the maximum permissable length of the string.
+ * Sets the maximum permissible length of the string.
*
* @param maxLength
* the length to set.
@@ -159,7 +128,7 @@ public class StringLengthValidator implements Validator {
}
/**
- * Sets the minimum permissable length.
+ * Sets the minimum permissible length.
*
* @param minLength
* the length to set.
@@ -171,23 +140,4 @@ public class StringLengthValidator implements Validator {
this.minLength = minLength;
}
- /**
- * Gets the message to be displayed in case the value does not validate.
- *
- * @return the Error Message.
- */
- public String getErrorMessage() {
- return errorMessage;
- }
-
- /**
- * Sets the message to be displayer in case the value does not validate.
- *
- * @param errorMessage
- * the Error Message to set.
- */
- public void setErrorMessage(String errorMessage) {
- this.errorMessage = errorMessage;
- }
-
}
diff --git a/src/com/itmill/toolkit/tests/validation/TestValidators.java b/src/com/itmill/toolkit/tests/validation/TestValidators.java
new file mode 100644
index 0000000000..2a75bdd406
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/validation/TestValidators.java
@@ -0,0 +1,163 @@
+package com.itmill.toolkit.tests.validation;
+
+import com.itmill.toolkit.data.Validator;
+import com.itmill.toolkit.data.validator.AbstractStringValidator;
+import com.itmill.toolkit.data.validator.CompositeValidator;
+import com.itmill.toolkit.data.validator.DoubleValidator;
+import com.itmill.toolkit.data.validator.EmailValidator;
+import com.itmill.toolkit.data.validator.IntegerValidator;
+import com.itmill.toolkit.data.validator.RegexpValidator;
+import com.itmill.toolkit.data.validator.StringLengthValidator;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Form;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.VerticalLayout;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+import com.itmill.toolkit.ui.Window.Notification;
+
+public class TestValidators extends TestBase {
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 680;
+ }
+
+ @Override
+ protected String getDescription() {
+ return "This test verifies that various validators work correctly";
+ }
+
+ @Override
+ public void setup() {
+ final Form form = new Form(new VerticalLayout());
+
+ // simple validators
+
+ TextField tf = new TextField("A field, must contain 1-2 chars");
+ tf
+ .addValidator(new StringLengthValidator("Invalid length", 1, 2,
+ false));
+ tf.setRequired(true);
+ tf.setValue("ab");
+ form.addField("a", tf);
+
+ tf = new TextField("A field, must contain an integer");
+ tf.addValidator(new IntegerValidator("Invalid integer {0}"));
+ tf.setRequired(true);
+ tf.setValue("123");
+ form.addField("b", tf);
+
+ tf = new TextField("A field, must contain an integer or be empty");
+ tf.addValidator(new IntegerValidator("Invalid integer {0}"));
+ tf.setValue("-321");
+ form.addField("c", tf);
+
+ tf = new TextField(
+ "A field, must contain a floating point number or be empty");
+ tf.addValidator(new DoubleValidator("Invalid double {0}"));
+ tf.setValue("-123.45e6");
+ form.addField("d", tf);
+
+ tf = new TextField(
+ "A field, must contain an e-mail address or be empty");
+ tf.addValidator(new EmailValidator("Invalid e-mail address {0}"));
+ tf.setValue("a.b@example.com");
+ form.addField("e", tf);
+
+ // regular expressions
+
+ tf = new TextField("A field, must match the regular expression a.*b.*c");
+ tf.addValidator(new RegexpValidator("a.*b.*c",
+ "{0} does not match the regular expression"));
+ tf.setValue("aagsabeqgc");
+ form.addField("f", tf);
+
+ tf = new TextField(
+ "A field, must contain the regular expression a.*b.*c");
+ tf.addValidator(new RegexpValidator("a.*b.*c", false,
+ "{0} does not contain the regular expression"));
+ tf.setValue("aagsabeqgc");
+ form.addField("g", tf);
+
+ tf = new TextField(
+ "A field, must match the regular expression ^a.*b.*c$");
+ tf.addValidator(new RegexpValidator("^a.*b.*c$", false,
+ "{0} does not match the regular expression with ^ and $"));
+ tf.setValue("aagsabeqgc");
+ form.addField("h", tf);
+
+ tf = new TextField(
+ "A field, must contain the regular expression ^a.*b.*c$");
+ tf.addValidator(new RegexpValidator("^a.*b.*c$", false,
+ "{0} does not contain the regular expression with ^ and $"));
+ tf.setValue("aagsabeqgc");
+ form.addField("i", tf);
+
+ // TODO CompositeValidator
+ tf = new TextField(
+ "A field, must be a floating point number with 4-5 chars");
+ CompositeValidator cv = new CompositeValidator(
+ CompositeValidator.MODE_AND,
+ "The field must contain a floating point number with 4-5 characters");
+ cv
+ .addValidator(new StringLengthValidator(
+ "String length of '{0}' should be 4-5 characters", 4,
+ 5, false));
+ cv.addValidator(new DoubleValidator(
+ "{0} must be a floating point number"));
+ tf.addValidator(cv);
+ tf.setValue("12.34");
+ form.addField("j", tf);
+
+ // Postal code that must be 5 digits (10000-99999).
+ tf = new TextField("Postal Code");
+ tf.setColumns(5);
+
+ // Create the validator - this would be even easier with RegexpValidator
+ Validator postalCodeValidator = new AbstractStringValidator(
+ "Postal code must be a number 10000-99999.") {
+ @Override
+ protected boolean isValidString(String value) {
+ return value.matches("[1-9][0-9]{4}");
+ }
+ };
+ tf.addValidator(postalCodeValidator);
+ tf.setValue("12345");
+ form.addField("k", tf);
+
+ Button b = new Button("Commit", new ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ try {
+ form.commit();
+ if (form.isValid()) {
+ getMainWindow().showNotification(
+ "OK! Form validated and no error was thrown",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "Form is invalid but no exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ }
+ } catch (Exception e) {
+ if (form.isValid()) {
+ getMainWindow().showNotification(
+ "Form is valid but an exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "OK! Error was thrown for an invalid input",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+
+ }
+ }
+ }
+
+ });
+
+ addComponent(form);
+ addComponent(b);
+ }
+}