]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix for #2568 - Added IntegerValidator to Sampler FormExample
authorArtur Signell <artur.signell@itmill.com>
Wed, 11 Feb 2009 17:50:05 +0000 (17:50 +0000)
committerArtur Signell <artur.signell@itmill.com>
Wed, 11 Feb 2009 17:50:05 +0000 (17:50 +0000)
svn changeset:6808/svn branch:trunk

src/com/itmill/toolkit/demo/sampler/features/form/FormPojoExample.java

index 65ff56da2f62999d12dd969112182abcf5e748aa..f350aed61363ddbb64a76412e3385a7890dfb6eb 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Date;
 import java.util.UUID;
 
 import com.itmill.toolkit.data.Item;
+import com.itmill.toolkit.data.Validator;
 import com.itmill.toolkit.data.util.BeanItem;
 import com.itmill.toolkit.data.validator.StringLengthValidator;
 import com.itmill.toolkit.demo.sampler.ExampleUtil;
@@ -139,6 +140,8 @@ public class FormPojoExample extends VerticalLayout {
                         "Password must be 6-20 characters", 6, 20, false));
             } else if ("shoesize".equals(propertyId)) {
                 TextField tf = (TextField) f;
+                tf.addValidator(new IntegerValidator(
+                        "Shoe size must be an Integer"));
                 tf.setWidth("2em");
             } else if ("uuid".equals(propertyId)) {
                 TextField tf = (TextField) f;
@@ -215,4 +218,32 @@ public class FormPojoExample extends VerticalLayout {
         }
 
     }
+
+    public class IntegerValidator implements Validator {
+
+        private String message;
+
+        public IntegerValidator(String message) {
+            this.message = message;
+        }
+
+        public boolean isValid(Object value) {
+            if (value == null || !(value instanceof String)) {
+                return false;
+            }
+            try {
+                Integer.parseInt((String) value);
+            } catch (Exception e) {
+                return false;
+            }
+            return true;
+        }
+
+        public void validate(Object value) throws InvalidValueException {
+            if (!isValid(value)) {
+                throw new InvalidValueException(message);
+            }
+        }
+
+    }
 }