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/02 23:08:38 UTC

[2/2] git commit: Re-implement regexp validation

Re-implement regexp validation


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

Branch: refs/heads/5.4-js-rewrite
Commit: 12d4d53e9ddbc3f75aab6e1f9bb27deb029003f2
Parents: 0e4027a
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Nov 2 15:03:41 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Nov 2 15:03:41 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/validation.coffee        |   13 ++++++++++
 .../apache/tapestry5/services/TapestryModule.java  |    7 +----
 .../org/apache/tapestry5/validator/Regexp.java     |   19 ++++++++++++--
 .../org/apache/tapestry5/validator/RegexpTest.java |    6 ++--
 4 files changed, 33 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/12d4d53e/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee
index 4207b9d..2783e3c 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/validation.coffee
@@ -19,6 +19,8 @@
 define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fields"],
   (_, spi, events, utils, messages) ->
 
+    REGEXP_META = "t5:regular-expression"
+
     minus = messages "decimal-symbols.minus"
     grouping = messages "decimal-symbols.group"
     decimal = messages "decimal-symbols.decimal"
@@ -128,5 +130,16 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie
       if memo.translated < min
         memo.error = (this.attribute "data-min-message") or "TOO SMALL"
 
+    spi.onDocument events.field.validate, "[data-validate-regexp]", (event, memo) ->
+
+      # Cache the compiled regular expression.
+      re = this.meta REGEXP_META
+      unless re
+        re = new RegExp(this.attribute "data-validate-regexp")
+        this.meta REGEXP_META, re
+
+      unless re.test memo.translated
+        memo.error = (this.attribute "data-regexp-message") or "INVALID"
+
     # Export the number parser, just to be nice (and to support some testing).
     return { parseNumber }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/12d4d53e/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 1e3e536..5f7f33f 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
@@ -83,7 +83,6 @@ import org.apache.tapestry5.services.dynamic.DynamicTemplate;
 import org.apache.tapestry5.services.dynamic.DynamicTemplateParser;
 import org.apache.tapestry5.services.javascript.JavaScriptModule;
 import org.apache.tapestry5.services.javascript.JavaScriptStack;
-import org.apache.tapestry5.services.javascript.JavaScriptSupport;
 import org.apache.tapestry5.services.javascript.ModuleManager;
 import org.apache.tapestry5.services.linktransform.ComponentEventLinkTransformer;
 import org.apache.tapestry5.services.linktransform.LinkTransformer;
@@ -773,7 +772,7 @@ public final class TapestryModule
         configuration.addInstance("maxlength", MaxLength.class);
         configuration.addInstance("min", Min.class);
         configuration.addInstance("max", Max.class);
-        configuration.add("regexp", new Regexp());
+        configuration.addInstance("regexp", Regexp.class);
         configuration.add("email", new Email());
         configuration.add("none", new None());
     }
@@ -1928,8 +1927,6 @@ public final class TapestryModule
         {
             public void renderMarkup(MarkupWriter writer, JSONObject reply, PartialMarkupRenderer renderer)
             {
-                JavaScriptSupport javascriptSupport = environment.peekRequired(JavaScriptSupport.class);
-
                 ClientBehaviorSupportImpl support = new ClientBehaviorSupportImpl();
 
                 environment.push(ClientBehaviorSupport.class, support);
@@ -1937,8 +1934,6 @@ public final class TapestryModule
                 renderer.renderMarkup(writer, reply);
 
                 environment.pop(ClientBehaviorSupport.class);
-
-                support.commit();
             }
         };
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/12d4d53e/tapestry-core/src/main/java/org/apache/tapestry5/validator/Regexp.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/validator/Regexp.java b/tapestry-core/src/main/java/org/apache/tapestry5/validator/Regexp.java
index 3d28d69..1e99163 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/validator/Regexp.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/validator/Regexp.java
@@ -19,15 +19,19 @@ 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.Matcher;
 import java.util.regex.Pattern;
 
+/**
+ * Enforces that the input matches a provided regular expression.
+ */
 public class Regexp extends AbstractValidator<Pattern, String>
 {
-    public Regexp()
+    public Regexp(JavaScriptSupport javaScriptSupport)
     {
-        super(Pattern.class, String.class, "regexp", null);
+        super(Pattern.class, String.class, "regexp", javaScriptSupport);
     }
 
     private String buildMessage(MessageFormatter formatter, Field field, Pattern constraintValue)
@@ -38,8 +42,17 @@ public class Regexp extends AbstractValidator<Pattern, String>
     public void render(Field field, Pattern constraintValue, MessageFormatter formatter, MarkupWriter writer,
                        FormSupport formSupport)
     {
+        if (formSupport.isClientValidationEnabled())
+        {
+            javaScriptSupport.require("core/validation");
+
+            writer.getElement().attributes("data-validation", "true",
+                    "data-validate-regexp", constraintValue.pattern(),
+                    "data-regexp-message", buildMessage(formatter, field, constraintValue));
+        }
+
         formSupport.addValidation(field, "regexp", buildMessage(formatter, field, constraintValue),
-                                  constraintValue.pattern());
+                constraintValue.pattern());
     }
 
     public void validate(Field field, Pattern constraintValue, MessageFormatter formatter, String value)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/12d4d53e/tapestry-core/src/test/java/org/apache/tapestry5/validator/RegexpTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/validator/RegexpTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/validator/RegexpTest.java
index 58ceb57..48d1fd5 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/validator/RegexpTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/validator/RegexpTest.java
@@ -1,4 +1,4 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 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.
@@ -36,7 +36,7 @@ public class RegexpTest extends InternalBaseTestCase
 
         replay();
 
-        Regexp validator = new Regexp();
+        Regexp validator = new Regexp(null);
 
         validator.validate(field, constraint, formatter, "1234");
 
@@ -57,7 +57,7 @@ public class RegexpTest extends InternalBaseTestCase
 
         replay();
 
-        Regexp validator = new Regexp();
+        Regexp validator = new Regexp(null);
 
         try
         {