You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2012/11/03 00:30:48 UTC

[1/5] git commit: Implement client-side validation of email addresses (for the first time)

Updated Branches:
  refs/heads/5.4-js-rewrite 658142934 -> 1778660c6


Implement client-side validation of email addresses (for the first time)


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/1778660c
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/1778660c
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/1778660c

Branch: refs/heads/5.4-js-rewrite
Commit: 1778660c6b67bfe41c4c4fac49bbaab4a61271da
Parents: 2b39d97
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Nov 2 16:30:18 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Nov 2 16:30:18 2012 -0700

----------------------------------------------------------------------
 .../apache/tapestry5/services/TapestryModule.java  |    5 ++-
 .../java/org/apache/tapestry5/validator/Email.java |   17 ++++++++++++--
 .../integration/app1/data/IncidentData.java        |    3 +-
 .../integration/app1/data/RegistrationData.java    |   11 ++++++---
 .../org/apache/tapestry5/validator/EmailTest.java  |    6 ++--
 5 files changed, 29 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1778660c/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
index 5f7f33f..813a036 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
@@ -765,7 +765,8 @@ public final class TapestryModule
      * <li>none</li>
      * </ul>
      */
-    public static void contributeFieldValidatorSource(MappedConfiguration<String, Validator> configuration)
+    @Contribute(FieldValidatorSource.class)
+    public static void setupCoreFrameworkValidators(MappedConfiguration<String, Validator> configuration)
     {
         configuration.addInstance("required", Required.class);
         configuration.addInstance("minlength", MinLength.class);
@@ -773,7 +774,7 @@ public final class TapestryModule
         configuration.addInstance("min", Min.class);
         configuration.addInstance("max", Max.class);
         configuration.addInstance("regexp", Regexp.class);
-        configuration.add("email", new Email());
+        configuration.addInstance("email", Email.class);
         configuration.add("none", new None());
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1778660c/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java b/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
index c4bcb30..7c7e7fc 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/validator/Email.java
@@ -19,11 +19,14 @@ import org.apache.tapestry5.MarkupWriter;
 import org.apache.tapestry5.ValidationException;
 import org.apache.tapestry5.ioc.MessageFormatter;
 import org.apache.tapestry5.services.FormSupport;
+import org.apache.tapestry5.services.javascript.JavaScriptSupport;
 
 import java.util.regex.Pattern;
 
 /**
  * A validator that checks if a given string is well-formed email address. This validator is not configurable.
+ * <p/>
+ * Starting with release 5.4, this validator also performs client-side validation.
  */
 public class Email extends AbstractValidator<Void, String>
 {
@@ -36,14 +39,22 @@ public class Email extends AbstractValidator<Void, String>
     private static final Pattern PATTERN = Pattern
             .compile("^" + ATOM + "+(\\." + ATOM + "+)*@" + DOMAIN + "|" + IP_DOMAIN + ")$", Pattern.CASE_INSENSITIVE);
 
-    public Email()
+    public Email(JavaScriptSupport javaScriptSupport)
     {
-        super(null, String.class, "invalid-email", null);
+        super(null, String.class, "invalid-email", javaScriptSupport);
     }
 
-    public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter markupWriter,
+    public void render(Field field, Void constraintValue, MessageFormatter formatter, MarkupWriter writer,
                        FormSupport formSupport)
     {
+        if (formSupport.isClientValidationEnabled())
+        {
+            javaScriptSupport.require("core/validation");
+
+            writer.getElement().attributes("data-validation", "true",
+                    "data-validate-regexp", PATTERN.pattern(),
+                    "data-regexp-message", buildMessage(formatter, field));
+        }
     }
 
     private String buildMessage(MessageFormatter formatter, Field field)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1778660c/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/IncidentData.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/IncidentData.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/IncidentData.java
index 33feee6..c3e6e9c 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/IncidentData.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/IncidentData.java
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2012 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@ public class IncidentData implements Serializable
 
     private Department department;
 
+    @Validate("email")
     public String getEmail()
     {
         return email;

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1778660c/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/RegistrationData.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/RegistrationData.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/RegistrationData.java
index 1fad57c..1a5598e 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/RegistrationData.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/data/RegistrationData.java
@@ -1,4 +1,4 @@
-// Copyright 2007, 2008, 2010 The Apache Software Foundation
+// Copyright 2007, 2008, 2010, 2012 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,15 +14,15 @@
 
 package org.apache.tapestry5.integration.app1.data;
 
-import java.util.List;
-
 import org.apache.tapestry5.beaneditor.DataType;
 import org.apache.tapestry5.beaneditor.ReorderProperties;
 import org.apache.tapestry5.beaneditor.Validate;
 import org.apache.tapestry5.beaneditor.Width;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 
-@ReorderProperties("firstname,lastname,birthyear,sex")
+import java.util.List;
+
+@ReorderProperties("firstname,lastname,email,birthyear,sex")
 public class RegistrationData
 {
     private String lastName;
@@ -41,6 +41,9 @@ public class RegistrationData
 
     private List<String> roles = CollectionFactory.newList();
 
+    @Validate("email")
+    public String email;
+
     @Validate("min=1900,max=2007")
     @Width(4)
     public int getBirthYear()

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/1778660c/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
index 5ba10d4..cddeea8 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/validator/EmailTest.java
@@ -1,4 +1,4 @@
-// Copyright 2008 The Apache Software Foundation
+// Copyright 2008, 2012 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ public class EmailTest extends InternalBaseTestCase
 
         replay();
 
-        Email validator = new Email();
+        Email validator = new Email(null);
 
         validator.validate(field, null, formatter, "myemail@mail.com");
 
@@ -49,7 +49,7 @@ public class EmailTest extends InternalBaseTestCase
 
         replay();
 
-        Email validator = new Email();
+        Email validator = new Email(null);
 
         try
         {