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 21:36:43 UTC
[1/2] git commit: Re-implement minlength and maxlength validators
Updated Branches:
refs/heads/5.4-js-rewrite b33dba042 -> cd6a89da6
Re-implement minlength and maxlength validators
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/cd6a89da
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/cd6a89da
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/cd6a89da
Branch: refs/heads/5.4-js-rewrite
Commit: cd6a89da6ce656604eeade980f4e8c270974cb07
Parents: 33ace58
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Fri Nov 2 13:36:08 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Fri Nov 2 13:36:08 2012 -0700
----------------------------------------------------------------------
.../META-INF/modules/core/validation.coffee | 24 ++++++++++++--
.../apache/tapestry5/services/TapestryModule.java | 4 +-
.../org/apache/tapestry5/validator/MaxLength.java | 18 +++++++----
.../org/apache/tapestry5/validator/MinLength.java | 12 +++++--
.../apache/tapestry5/validator/MaxLengthTest.java | 6 ++--
.../apache/tapestry5/validator/MinLengthTest.java | 6 ++--
6 files changed, 49 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cd6a89da/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 536a188..dc5bea6 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
@@ -82,6 +82,9 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie
return Number canonical
+ error = (field, message) ->
+ field.trigger events.field.showValidationError, { message }
+
translate = (field, memo, isInteger) ->
try
result = parseNumber memo.value, isInteger
@@ -90,17 +93,17 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie
throw messages "core-input-not-numeric"
memo.translated = result
- catch error
+ catch e
memo.error = true
- message = (field.attribute "data-translation-message") or error.message or "ERROR"
+ message = (field.attribute "data-translation-message") or e.message or "ERROR"
- field.trigger events.field.showValidationError, { message }
+ error field, message
spi.onDocument events.field.optional, "[data-optionality=required]", (event, memo) ->
if utils.isBlank memo.value
message = (this.attribute "data-required-message") || "REQUIRED"
- this.trigger events.field.showValidationError, { message }
+ error this, message
memo.error = true
return false
@@ -110,6 +113,19 @@ define ["_", "core/spi", "core/events", "core/utils", "core/messages", "core/fie
spi.onDocument events.field.translate, "[data-translation=integer]", (event, memo) ->
translate this, memo, true
+ spi.onDocument events.field.validate, "[data-validate-min-length]", (event, memo) ->
+ min = parseInt this.attribute "data-validate-min-length"
+
+ if memo.translated.length < min
+ memo.error = true
+ error this, (this.attribute "data-min-length-message") or "TOO SHORT"
+
+ spi.onDocument events.field.validate, "[data-validate-max-length]", (event, memo) ->
+ min = parseInt this.attribute "data-validate-max-length"
+
+ if memo.translated.length > min
+ memo.error = true
+ error this, (this.attribute "data-max-length-message") or "TOO LONG"
# 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/cd6a89da/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 cb1db09..49e2c11 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
@@ -769,8 +769,8 @@ public final class TapestryModule
public static void contributeFieldValidatorSource(MappedConfiguration<String, Validator> configuration)
{
configuration.addInstance("required", Required.class);
- configuration.add("minlength", new MinLength());
- configuration.add("maxlength", new MaxLength());
+ configuration.addInstance("minlength", MinLength.class);
+ configuration.addInstance("maxlength", MaxLength.class);
configuration.add("min", new Min());
configuration.add("max", new Max());
configuration.add("regexp", new Regexp());
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cd6a89da/tapestry-core/src/main/java/org/apache/tapestry5/validator/MaxLength.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/validator/MaxLength.java b/tapestry-core/src/main/java/org/apache/tapestry5/validator/MaxLength.java
index 44e4eae..db0faf1 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/validator/MaxLength.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/validator/MaxLength.java
@@ -19,12 +19,16 @@ 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;
+/**
+ * Validates that a string value has not exceeded a maximum length.
+ */
public final class MaxLength extends AbstractValidator<Integer, String>
{
- public MaxLength()
+ public MaxLength(JavaScriptSupport javaScriptSupport)
{
- super(Integer.class, String.class, "maximum-string-length", null);
+ super(Integer.class, String.class, "maximum-string-length", javaScriptSupport);
}
public void validate(Field field, Integer constraintValue, MessageFormatter formatter, String value)
@@ -42,9 +46,11 @@ public final class MaxLength extends AbstractValidator<Integer, String>
public void render(Field field, Integer constraintValue, MessageFormatter formatter, MarkupWriter writer,
FormSupport formSupport)
{
- // TODO: write a maxlength attribute into the element? But that's only for
- // textfield, not for textarea.
-
- formSupport.addValidation(field, "maxlength", buildMessage(formatter, field, constraintValue), constraintValue);
+ if (formSupport.isClientValidationEnabled())
+ {
+ writer.getElement().attributes("data-validate", "true",
+ "data-validate-max-length", constraintValue.toString(),
+ "data-max-length-message", buildMessage(formatter, field, constraintValue));
+ }
}
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cd6a89da/tapestry-core/src/main/java/org/apache/tapestry5/validator/MinLength.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/validator/MinLength.java b/tapestry-core/src/main/java/org/apache/tapestry5/validator/MinLength.java
index 8ae6c3c..e3af221 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/validator/MinLength.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/validator/MinLength.java
@@ -19,15 +19,16 @@ 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;
/**
* Validates that a string value has a minimum length.
*/
public final class MinLength extends AbstractValidator<Integer, String>
{
- public MinLength()
+ public MinLength(JavaScriptSupport javaScriptSupport)
{
- super(Integer.class, String.class, "minimum-string-length", null);
+ super(Integer.class, String.class, "minimum-string-length", javaScriptSupport);
}
public void validate(Field field, Integer constraintValue, MessageFormatter formatter, String value)
@@ -45,6 +46,11 @@ public final class MinLength extends AbstractValidator<Integer, String>
public void render(Field field, Integer constraintValue, MessageFormatter formatter, MarkupWriter writer,
FormSupport formSupport)
{
- formSupport.addValidation(field, "minlength", buildMessage(formatter, field, constraintValue), constraintValue);
+ if (formSupport.isClientValidationEnabled())
+ {
+ writer.getElement().attributes("data-validate", "true",
+ "data-validate-min-length", constraintValue.toString(),
+ "data-min-length-message", buildMessage(formatter, field, constraintValue));
+ }
}
}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cd6a89da/tapestry-core/src/test/java/org/apache/tapestry5/validator/MaxLengthTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/validator/MaxLengthTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/validator/MaxLengthTest.java
index 0d1daac..b077c6f 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/validator/MaxLengthTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/validator/MaxLengthTest.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.
@@ -31,7 +31,7 @@ public class MaxLengthTest extends InternalBaseTestCase
replay();
- MaxLength validator = new MaxLength();
+ MaxLength validator = new MaxLength(null);
validator.validate(field, value.length(), formatter, value);
@@ -52,7 +52,7 @@ public class MaxLengthTest extends InternalBaseTestCase
replay();
- MaxLength validator = new MaxLength();
+ MaxLength validator = new MaxLength(null);
try
{
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/cd6a89da/tapestry-core/src/test/java/org/apache/tapestry5/validator/MinLengthTest.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/validator/MinLengthTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/validator/MinLengthTest.java
index 9996d14..0ba9af3 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/validator/MinLengthTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/validator/MinLengthTest.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.
@@ -32,7 +32,7 @@ public class MinLengthTest extends InternalBaseTestCase
replay();
- MinLength validator = new MinLength();
+ MinLength validator = new MinLength(null);
validator.validate(field, value.length(), formatter, value);
@@ -53,7 +53,7 @@ public class MinLengthTest extends InternalBaseTestCase
replay();
- MinLength validator = new MinLength();
+ MinLength validator = new MinLength(null);
try
{