From f7dcedfd372eb05d68feec7bc0dc7a3e0a59d721 Mon Sep 17 00:00:00 2001
From: Henri Sara
+ * If the validation fails, the exception thrown contains the error message with
+ * its argument 0 replaced with the string being validated.
+ *
+ * Null and empty string values are always accepted. To disallow empty
+ * values, set the field being validated as required.
+ *
+ * If the validation fails, the exception thrown contains the error message with
+ * its argument 0 replaced with the toString() of the object being validated.
+ * AND
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 ListAND
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
@@ -203,17 +199,6 @@ public class CompositeValidator implements Validator {
return null;
}
- /**
- * 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.
*
@@ -254,23 +239,22 @@ public class CompositeValidator implements Validator {
* validators of given type null is returned.
*
+ * For the Java regular expression syntax, see + * {@link java.util.regex.Pattern#sum} + *
+ *+ * See {@link com.itmill.toolkit.data.validator.AbstractStringValidator} for + * more information. + *
+ * + * @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; - /** * ThisStringLengthValidator
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) {
@@ -56,37 +53,6 @@ public class StringLengthValidator implements Validator {
setNullAllowed(allowNull);
}
- /**
- * 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.
*
@@ -116,12 +82,13 @@ public class StringLengthValidator implements Validator {
* @return true
if allows null string, otherwise
* false
.
*/
+ @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);
+ }
+}
--
2.39.5