]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added possibility to add form-level validators
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Mon, 19 May 2008 12:00:48 +0000 (12:00 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Mon, 19 May 2008 12:00:48 +0000 (12:00 +0000)
svn changeset:4549/svn branch:trunk

src/com/itmill/toolkit/tests/tickets/Ticket736.java

index 4410b634576a90de3db53f987166c25e965c89cb..7a484aa34342a385de268a04f3a634f26c0fa2bc 100644 (file)
@@ -21,7 +21,7 @@ public class Ticket736 extends Application {
         setMainWindow(mainWin);
 
         // Create form for editing address
-        Form f = new Form();
+        final Form f = new Form();
         f.setItemDataSource(new BeanItem(address, new String[] { "name",
                 "street", "zip", "city", "state", "country" }));
         f.setCaption("Office address");
@@ -68,6 +68,22 @@ public class Ticket736 extends Application {
                     }
                 }));
 
+        final AddressValidator av = new AddressValidator();
+        mainWin.addComponent(new Button("Add addressvalidator",
+                new Button.ClickListener() {
+
+                    public void buttonClick(ClickEvent event) {
+                        f.addValidator(av);
+                    }
+                }));
+        mainWin.addComponent(new Button("Remove addressvalidator",
+                new Button.ClickListener() {
+
+                    public void buttonClick(ClickEvent event) {
+                        f.removeValidator(av);
+                    }
+                }));
+
     }
 
     /** Address pojo. */
@@ -154,6 +170,33 @@ public class Ticket736 extends Application {
         }
     }
 
+    class AddressValidator implements Validator {
+
+        public boolean isValid(Object value) {
+            if (!(value instanceof Address)) {
+                return false;
+            }
+            Address a = (Address) value;
+            if (a.getCity() == null || ("" + a.getCity()).length() < 1) {
+                return false;
+            }
+            if (a.getStreet() == null || ("" + a.getStreet()).length() < 1) {
+                return false;
+            }
+            if (a.getZip() == null || ("" + a.getZip()).length() < 5) {
+                return false;
+            }
+            return true;
+        }
+
+        public void validate(Object value) throws InvalidValueException {
+            if (!isValid(value)) {
+                throw new InvalidValueException(
+                        "Address should at least have street, zip and city set");
+            }
+        }
+    }
+
     /** Simple state validator */
     class IsValidState implements Validator {