You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by jk...@apache.org on 2006/07/02 01:19:28 UTC

svn commit: r418519 - in /tapestry/tapestry4/trunk/framework/src: java/org/apache/tapestry/form/validator/ js/tapestry/form/ test/org/apache/tapestry/form/validator/

Author: jkuhnert
Date: Sat Jul  1 16:19:27 2006
New Revision: 418519

URL: http://svn.apache.org/viewvc?rev=418519&view=rev
Log:
Finished off remaining client side validation upgrades. Will save new logic for another time.

Removed:
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/NumberValidator.js
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/RegExValidator.js
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/StringValidator.js
Modified:
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/BaseValidator.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxDate.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Min.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinDate.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinLength.java
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Pattern.java
    tapestry/tapestry4/trunk/framework/src/js/tapestry/form/validation.js
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMax.java
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMin.java
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/BaseValidator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/BaseValidator.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/BaseValidator.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/BaseValidator.java Sat Jul  1 16:19:27 2006
@@ -19,6 +19,8 @@
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
+import org.apache.tapestry.form.TranslatedField;
+import org.apache.tapestry.form.translator.Translator;
 import org.apache.tapestry.json.JSONArray;
 import org.apache.tapestry.json.JSONObject;
 
@@ -115,5 +117,24 @@
             profile.put(key, new JSONArray());
         
         profile.accumulate(key, value);
+    }
+    
+    /**
+     * Used to grab the corresponding {@link Translator} for 
+     * the field, if one exists.
+     * @param field
+     * @return The translator, or null if the required translator type 
+     *          doesn't exist.
+     */
+    public Translator getFieldTranslator(IFormComponent field, Class clazz)
+    {
+        if (TranslatedField.class.isAssignableFrom(field.getClass())) {
+            Translator trans = ((TranslatedField)field).getTranslator();
+            if (clazz.isInstance(trans)) {
+                return trans;
+            }
+        }
+        
+        return null;
     }
 }

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxDate.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxDate.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxDate.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxDate.java Sat Jul  1 16:19:27 2006
@@ -20,10 +20,8 @@
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
-import org.apache.tapestry.form.TranslatedField;
 import org.apache.tapestry.form.ValidationMessages;
 import org.apache.tapestry.form.translator.DateTranslator;
-import org.apache.tapestry.form.translator.Translator;
 import org.apache.tapestry.json.JSONLiteral;
 import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.util.Strftime;
@@ -55,7 +53,7 @@
             throws ValidatorException
     {
         Date date = (Date) object;
-        DateTranslator translator = getFieldTranslator(field);
+        DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
         
         if (date.after(_maxDate))
             throw new ValidatorException(buildMessage(messages, field, translator),
@@ -81,7 +79,7 @@
     {
         // TODO: This is a little hacky, but validators need to be able to cooperate
         // with translators during client side validation as well
-        DateTranslator translator = getFieldTranslator(field);
+        DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
         if (translator == null)
             return;
         
@@ -105,25 +103,6 @@
         
         setProfileProperty(field, profile, 
                 ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
-    }
-    
-    /**
-     * Used to grab the corresponding {@link DateTranslator} for 
-     * the field, if one exists.
-     * @param field
-     * @return The translator, or null if the required translator type 
-     *          doesn't exist.
-     */
-    private DateTranslator getFieldTranslator(IFormComponent field)
-    {
-        if (TranslatedField.class.isAssignableFrom(field.getClass())) {
-            Translator trans = ((TranslatedField)field).getTranslator();
-            if (DateTranslator.class.isInstance(trans)) {
-                return (DateTranslator)trans;
-            }
-        }
-        
-        return null;
     }
     
     public void setMaxDate(Date minDate)

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MaxLength.java Sat Jul  1 16:19:27 2006
@@ -16,10 +16,12 @@
 
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
-import org.apache.tapestry.TapestryUtils;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONLiteral;
+import org.apache.tapestry.json.JSONObject;
+import org.apache.tapestry.valid.ValidationConstants;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -71,17 +73,19 @@
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
             FormComponentContributorContext context, IFormComponent field)
     {
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
-
-        StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_max_length(event, '");
-        buffer.append(field.getClientId());
-        buffer.append("', ");
-        buffer.append(_maxLength);
-        buffer.append(", ");
-        buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
-        buffer.append("); }");
-
-        context.addSubmitHandler(buffer.toString());
+        JSONObject profile = context.getProfile();
+        
+        if (!profile.has(ValidationConstants.CONSTRAINTS)) {
+            profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
+        }
+        JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
+        
+        cons.put(field.getClientId(), 
+                new JSONLiteral("[dojo.validate.isText,{"
+                        + "maxlength:" + _maxLength + "}]"));
+        
+        setProfileProperty(field, profile, 
+                ValidationConstants.CONSTRAINTS, buildMessage(context, field));
     }
 
 }

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Min.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Min.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Min.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Min.java Sat Jul  1 16:19:27 2006
@@ -14,12 +14,16 @@
 
 package org.apache.tapestry.form.validator;
 
+import java.text.DecimalFormatSymbols;
+
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
-import org.apache.tapestry.TapestryUtils;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONLiteral;
+import org.apache.tapestry.json.JSONObject;
+import org.apache.tapestry.valid.ValidationConstants;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -65,23 +69,28 @@
                 new Object[]
                 { field.getDisplayName(), new Double(_min) });
     }
-
+    
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
             FormComponentContributorContext context, IFormComponent field)
     {
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/NumberValidator.js");
-
-        String message = buildMessage(context, field);
-
-        StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_min_number(event, '");
-        buffer.append(field.getClientId());
-        buffer.append("', ");
-        buffer.append(_min);
-        buffer.append(", ");
-        buffer.append(TapestryUtils.enquote(message));
-        buffer.append("); }");
-
-        context.addSubmitHandler(buffer.toString());
+        JSONObject profile = context.getProfile();
+        
+        if (!profile.has(ValidationConstants.CONSTRAINTS)) {
+            profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
+        }
+        JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
+        
+        // TODO: Should find some way to provide this globally and cache.
+        DecimalFormatSymbols symbols = new DecimalFormatSymbols(context.getLocale());
+        
+        cons.put(field.getClientId(), 
+                new JSONLiteral("[dojo.validate.isInRange,{"
+                        + "min:" + _min + ","
+                        + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator())
+                        + "]"));
+        
+        setProfileProperty(field, profile, 
+                ValidationConstants.CONSTRAINTS, buildMessage(context, field));
     }
 
     public void setMin(double min)

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinDate.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinDate.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinDate.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinDate.java Sat Jul  1 16:19:27 2006
@@ -16,8 +16,16 @@
 
 import java.util.Date;
 
+import org.apache.tapestry.IMarkupWriter;
+import org.apache.tapestry.IRequestCycle;
+import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.form.translator.DateTranslator;
+import org.apache.tapestry.json.JSONLiteral;
+import org.apache.tapestry.json.JSONObject;
+import org.apache.tapestry.util.Strftime;
+import org.apache.tapestry.valid.ValidationConstants;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -41,29 +49,65 @@
     {
         super(initializer);
     }
-
+    
     public void setMinDate(Date minDate)
     {
         _minDate = minDate;
     }
-
+    
     public void validate(IFormComponent field, ValidationMessages messages, Object object)
-            throws ValidatorException
+    throws ValidatorException
     {
         Date date = (Date) object;
-
+        DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
+        
         if (date.before(_minDate))
-            throw new ValidatorException(buildMessage(messages, field),
+            throw new ValidatorException(buildMessage(messages, field, translator),
                     ValidationConstraint.TOO_SMALL);
-    }
 
-    private String buildMessage(ValidationMessages messages, IFormComponent field)
+    }
+    
+    private String buildMessage(ValidationMessages messages, IFormComponent field, 
+            DateTranslator translator)
     {
         return messages.formatValidationMessage(
                 getMessage(),
                 ValidationStrings.DATE_TOO_EARLY,
                 new Object[]
-                { field.getDisplayName(), _minDate });
+                           { field.getDisplayName(), 
+                    (translator != null) ? 
+                            translator.format(field, messages.getLocale(), _minDate)
+                            : _minDate});
+    }
+    
+    public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
+            FormComponentContributorContext context, IFormComponent field)
+    {
+        // TODO: This is a little hacky, but validators need to be able to cooperate
+        // with translators during client side validation as well
+        DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class);
+        if (translator == null)
+            return;
+        
+        JSONObject profile = context.getProfile();
+        
+        context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
+        
+        if (!profile.has(ValidationConstants.CONSTRAINTS)) {
+            profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
+        }
+        JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
+        
+        cons.put(field.getClientId(), 
+                new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
+                        + "min:" 
+                        + JSONObject.quote(translator.format(field, context.getLocale(), _minDate))
+                        + ","
+                        + "format:" 
+                        + JSONObject.quote(Strftime.convertToPosixFormat(translator.getPattern()))
+                        + "}]"));
+        
+        setProfileProperty(field, profile, 
+                ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator));
     }
-
 }

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinLength.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinLength.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/MinLength.java Sat Jul  1 16:19:27 2006
@@ -16,10 +16,12 @@
 
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
-import org.apache.tapestry.TapestryUtils;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONLiteral;
+import org.apache.tapestry.json.JSONObject;
+import org.apache.tapestry.valid.ValidationConstants;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -75,16 +77,18 @@
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
             FormComponentContributorContext context, IFormComponent field)
     {
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
-
-        StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_min_length(event, '");
-        buffer.append(field.getClientId());
-        buffer.append("', ");
-        buffer.append(_minLength);
-        buffer.append(", ");
-        buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
-        buffer.append("); }");
-
-        context.addSubmitHandler(buffer.toString());
+        JSONObject profile = context.getProfile();
+        
+        if (!profile.has(ValidationConstants.CONSTRAINTS)) {
+            profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
+        }
+        JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
+        
+        cons.put(field.getClientId(), 
+                new JSONLiteral("[dojo.validate.isText,{"
+                        + "minlength:" + _minLength + "}]"));
+        
+        setProfileProperty(field, profile, 
+                ValidationConstants.CONSTRAINTS, buildMessage(context, field));
     }
 }

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Pattern.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Pattern.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Pattern.java (original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/form/validator/Pattern.java Sat Jul  1 16:19:27 2006
@@ -16,11 +16,13 @@
 
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
-import org.apache.tapestry.TapestryUtils;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONLiteral;
+import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.util.RegexpMatcher;
+import org.apache.tapestry.valid.ValidationConstants;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -66,26 +68,27 @@
                 getMessage(),
                 ValidationStrings.REGEX_MISMATCH,
                 new Object[]
-                { field.getDisplayName() });
+                { _pattern, field.getDisplayName() });
     }
-
+    
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
             FormComponentContributorContext context, IFormComponent field)
     {
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
-
         String pattern = _matcher.getEscapedPatternString(_pattern);
-        String message = buildMessage(context, field);
-
-        StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_regex(event, '");
-        buffer.append(field.getClientId());
-        buffer.append("', '");
-        buffer.append(pattern);
-        buffer.append("', ");
-        buffer.append(TapestryUtils.enquote(message));
-        buffer.append("); }");
-
-        context.addSubmitHandler(buffer.toString());
+        
+        JSONObject profile = context.getProfile();
+        
+        if (!profile.has(ValidationConstants.CONSTRAINTS)) {
+            profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
+        }
+        JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
+        
+        cons.put(field.getClientId(), 
+                new JSONLiteral("[tapestry.form.validation.isValidPattern,\""
+                        + pattern + "\"]"));
+        
+        setProfileProperty(field, profile, 
+                ValidationConstants.CONSTRAINTS, buildMessage(context, field));
     }
 
     public void setPattern(String pattern)

Modified: tapestry/tapestry4/trunk/framework/src/js/tapestry/form/validation.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/js/tapestry/form/validation.js?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/js/tapestry/form/validation.js (original)
+++ tapestry/tapestry4/trunk/framework/src/js/tapestry/form/validation.js Sat Jul  1 16:19:27 2006
@@ -198,5 +198,19 @@
 							message:msg
 						}, node);
 		dialog.show();
+	},
+	
+	/**
+	 * Validates that the input value matches the given 
+	 * regexp pattern.
+	 * 
+	 * @param value The string value to be evaluated.
+	 * @param pattern The regexp pattern used to match against value.
+	 */
+	isValidPattern:function(value, pattern){
+		if (typeof value != "string" || typeof pattern != "string") { return false; }
+		
+		var re = new RegExp(pattern);
+		return re.test(value);
 	}
 }

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMax.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMax.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMax.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMax.java Sat Jul  1 16:19:27 2006
@@ -122,7 +122,7 @@
         { "My Field", new Double(20) }, "default message");
         
         replay();
-
+        
         new Max("max=20").renderContribution(writer, cycle, context, field);
 
         verify();

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMaxLength.java Sat Jul  1 16:19:27 2006
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry.form.validator;
 
+import static org.easymock.EasyMock.expect;
 import static org.testng.AssertJUnit.assertEquals;
 
 import org.apache.tapestry.IMarkupWriter;
@@ -21,6 +22,7 @@
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -98,22 +100,25 @@
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         IFormComponent field = newField("My Field", "myfield");
         
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
-
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
-
-        trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_LONG, new Object[]
-        { new Integer(20), "My Field" }, "default\\message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_max_length(event, 'myfield', 20, 'default\\\\message'); }");
-
+        
+        expect(context.getProfile()).andReturn(json);
+        
+        trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_LONG, 
+                new Object[] { new Integer(20), "My Field" }, "default\\message");
+        
         replay();
-
+        
         new MaxLength("maxLength=20").renderContribution(writer, cycle, context, field);
-
+        
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"default\\\\message\"},"
+                + "\"constraints\":{\"myfield\":[dojo.validate.isText,{maxlength:20}]}}",
+                json.toString());
     }
 }

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMin.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMin.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMin.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMin.java Sat Jul  1 16:19:27 2006
@@ -14,13 +14,18 @@
 
 package org.apache.tapestry.form.validator;
 
+import static org.easymock.EasyMock.expect;
 import static org.testng.AssertJUnit.assertEquals;
 
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -99,35 +104,52 @@
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         IFormComponent field = newField("My Field", "myfield");
         
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
         
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/NumberValidator.js");
+        Locale locale = Locale.FRANCE;
+        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
+        
+        expect(context.getLocale()).andReturn(locale);
+        
+        expect(context.getProfile()).andReturn(json);
         
         trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_SMALL, new Object[]
         { "My Field", new Double(20) }, "default message");
         
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_min_number(event, 'myfield', 20.0, 'default message'); }");
-        
         replay();
         
         new Min("min=20").renderContribution(writer, cycle, context, field);
-
+        
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"default message\"},"
+                + "\"constraints\":{\"myfield\":[dojo.validate.isInRange,{min:20.0,decimal:\""
+                + symbols.getDecimalSeparator() + "\"]}}",
+                json.toString());
     }
-
+    
     public void testRenderContributionCustomMessage()
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        
+        JSONObject json = new JSONObject();
+        
         IFormComponent field = newField("My Field", "myfield");
         
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
-
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/NumberValidator.js");
-
+        
+        Locale locale = Locale.FRANCE;
+        DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
+        
+        expect(context.getLocale()).andReturn(locale);
+        
+        expect(context.getProfile()).andReturn(json);
+        
         trainFormatMessage(
                 context,
                 "custom",
@@ -135,14 +157,17 @@
                 new Object[]
                 { "My Field", new Double(20) },
                 "custom\\message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_min_number(event, 'myfield', 20.0, 'custom\\\\message'); }");
-
+        
         replay();
-
+        
         new Min("min=20,message=custom").renderContribution(writer, cycle, context, field);
 
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"custom\\\\message\"},"
+                + "\"constraints\":{\"myfield\":[dojo.validate.isInRange,{min:20.0,decimal:\""
+                + symbols.getDecimalSeparator() + "\"]}}",
+                json.toString());
     }
+    
 }

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinDate.java Sat Jul  1 16:19:27 2006
@@ -14,15 +14,22 @@
 
 package org.apache.tapestry.form.validator;
 
+import static org.easymock.EasyMock.checkOrder;
+import static org.easymock.EasyMock.expect;
 import static org.testng.AssertJUnit.assertEquals;
 
 import java.util.Date;
+import java.util.Locale;
 
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
+import org.apache.tapestry.form.TranslatedField;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.form.translator.DateTranslator;
+import org.apache.tapestry.json.JSONObject;
+import org.apache.tapestry.util.Strftime;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -75,10 +82,10 @@
                 "default message");
 
         replay();
-
+        
         MinDate v = new MinDate();
         v.setMinDate(tomorrow);
-
+        
         try
         {
             v.validate(field, message, today);
@@ -126,18 +133,99 @@
 
         verify();
     }
-
-    public void testRenderComponentNoOp()
+    
+    public void testRenderContribution()
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
-        FormComponentContributorContext context = newContext();
-        IFormComponent field = newField();
-
+        JSONObject json = new JSONObject();
+        
+        TranslatedField field = newMock(TranslatedField.class);
+        checkOrder(field, false);
+        
+        Date minDate = new Date(System.currentTimeMillis() + ONE_DAY);
+        DateTranslator translator = new DateTranslator();
+        
+        expect(field.getTranslator()).andReturn(translator);
+        
+        expect(field.getClientId()).andReturn("myfield").anyTimes();
+        
+        expect(field.getDisplayName()).andReturn("My Field");
+        
+        FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
+        
+        Locale locale = Locale.ENGLISH;
+        expect(context.getLocale()).andReturn(locale).anyTimes();
+        
+        expect(context.getProfile()).andReturn(json);
+        
+        context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
+        
+        String strMin = translator.format(field, locale, minDate);
+        
+        trainFormatMessage(context, null, ValidationStrings.DATE_TOO_EARLY, 
+                new Object[] { "My Field", strMin }, "default message");
+        
         replay();
-
-        new MinDate().renderContribution(writer, cycle, context, field);
-
+        
+        new MinDate("minDate="+strMin).renderContribution(writer, cycle, context, field);
+        
+        verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"default message\"},"
+                + "\"constraints\":{\"myfield\":["
+                + "tapestry.form.datetime.isValidDate,{min:\""
+                + strMin + "\",format:"
+                + JSONObject.quote(Strftime.convertToPosixFormat(translator.getPattern())) 
+                + "}]}}",
+                json.toString());
+    }
+    
+    public void testRenderContributionCustomMessage()
+    {
+        IMarkupWriter writer = newWriter();
+        IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
+        TranslatedField field = newMock(TranslatedField.class);
+        checkOrder(field, false);
+        
+        Date minDate = new Date(System.currentTimeMillis() + ONE_DAY);
+        DateTranslator translator = new DateTranslator();
+        
+        expect(field.getTranslator()).andReturn(translator);
+        
+        expect(field.getClientId()).andReturn("myfield").anyTimes();
+        
+        expect(field.getDisplayName()).andReturn("My Field");
+        
+        FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
+        
+        Locale locale = Locale.ENGLISH;
+        expect(context.getLocale()).andReturn(locale).anyTimes();
+        
+        expect(context.getProfile()).andReturn(json);
+        
+        context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
+        
+        String strMin = translator.format(field, locale, minDate);
+        
+        trainFormatMessage(context, "custom", ValidationStrings.DATE_TOO_EARLY, 
+                new Object[] { "My Field", strMin }, 
+                "custom\\message");
+        
+        replay();
+        
+        new MinDate("minDate=" + strMin + ",message=custom").renderContribution(writer, cycle, context, field);
+        
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"custom\\\\message\"},"
+                + "\"constraints\":{\"myfield\":["
+                + "tapestry.form.datetime.isValidDate,{min:\""
+                + strMin + "\",format:"
+                + JSONObject.quote(Strftime.convertToPosixFormat(translator.getPattern())) 
+                + "}]}}",
+                json.toString());
     }
 }

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestMinLength.java Sat Jul  1 16:19:27 2006
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry.form.validator;
 
+import static org.easymock.EasyMock.expect;
 import static org.testng.AssertJUnit.assertEquals;
 
 import org.apache.tapestry.IMarkupWriter;
@@ -21,6 +22,7 @@
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
 import org.apache.tapestry.valid.ValidatorException;
@@ -99,57 +101,62 @@
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         IFormComponent field = newField("My Field", "myfield");
         
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
-
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
-
-        trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_SHORT, new Object[]
-        { new Integer(20), "My Field" }, "default message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_min_length(event, 'myfield', 20, 'default message'); }");
-
+        
+        expect(context.getProfile()).andReturn(json);
+        
+        trainFormatMessage(context, null, ValidationStrings.VALUE_TOO_SHORT, 
+                new Object[] { new Integer(25), "My Field" }, "default\\message");
+        
         replay();
-
-        new MinLength("minLength=20").renderContribution(writer, cycle, context, field);
-
+        
+        new MinLength("minLength=25").renderContribution(writer, cycle, context, field);
+        
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"default\\\\message\"},"
+                + "\"constraints\":{\"myfield\":[dojo.validate.isText,{minlength:25}]}}",
+                json.toString());
     }
-
+    
     public void testRenderContributionCustomMessage()
     {
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         IFormComponent field = newField("My Field", "customField");
         
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
 
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
-
+        expect(context.getProfile()).andReturn(json);
+        
         trainFormatMessage(
                 context,
                 "custom",
                 ValidationStrings.VALUE_TOO_SHORT,
-                new Object[]
-                { new Integer(25), "My Field" },
+                new Object[] { new Integer(25), "My Field" },
                 "custom\\message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_min_length(event, 'customField', 25, 'custom\\\\message'); }");
-
+        
         replay();
-
+        
         new MinLength("minLength=25,message=custom").renderContribution(
                 writer,
                 cycle,
                 context,
                 field);
-
+        
         verify();
+        
+        assertEquals("{\"customField\":{\"constraints\":\"custom\\\\message\"},"
+                + "\"constraints\":{\"customField\":[dojo.validate.isText,{minlength:25}]}}",
+                json.toString());
     }
-
+    
     public void testNotRequired()
     {
         assertEquals(false, new MinLength().isRequired());

Modified: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java?rev=418519&r1=418518&r2=418519&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java (original)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/form/validator/TestPattern.java Sat Jul  1 16:19:27 2006
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry.form.validator;
 
+import static org.easymock.EasyMock.expect;
 import static org.testng.AssertJUnit.assertEquals;
 
 import org.apache.tapestry.IMarkupWriter;
@@ -21,6 +22,7 @@
 import org.apache.tapestry.form.FormComponentContributorContext;
 import org.apache.tapestry.form.IFormComponent;
 import org.apache.tapestry.form.ValidationMessages;
+import org.apache.tapestry.json.JSONObject;
 import org.apache.tapestry.util.RegexpMatcher;
 import org.apache.tapestry.valid.ValidationConstraint;
 import org.apache.tapestry.valid.ValidationStrings;
@@ -51,12 +53,13 @@
 
     public void testFail()
     {
+        String pattern = "\\d+";
         IFormComponent field = newField("My Pattern");
         ValidationMessages messages = newMessages(
                 null,
                 ValidationStrings.REGEX_MISMATCH,
                 new Object[]
-                { "My Pattern" },
+                { pattern, "My Pattern" },
                 "default message");
 
         replay();
@@ -77,16 +80,17 @@
 
     public void testFailCustomMessage()
     {
+        String pattern = "\\d+";
         IFormComponent field = newField("My Pattern");
         ValidationMessages messages = newMessages(
                 "custom",
                 ValidationStrings.REGEX_MISMATCH,
                 new Object[]
-                { "My Pattern" },
+                { pattern, "My Pattern" },
                 "custom message");
-
+        
         replay();
-
+        
         try
         {
             new Pattern("pattern=\\d+,message=custom").validate(field, messages, "fred");
@@ -103,60 +107,67 @@
 
     public void testRenderContribution()
     {
-        String pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
-
+        String rawPattern = "\\d+";
+        String pattern = new RegexpMatcher().getEscapedPatternString(rawPattern);
+        
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
-
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
-
+        
         IFormComponent field = newField("Fred", "myfield");
-
-        trainFormatMessage(context, null, ValidationStrings.REGEX_MISMATCH, new Object[]
-        { "Fred" }, "default message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_regex(event, 'myfield', '"
-                        + pattern + "', 'default message'); }");
-
+        
+        expect(context.getProfile()).andReturn(json);
+        
+        trainFormatMessage(context, null, ValidationStrings.REGEX_MISMATCH, 
+                new Object[] { rawPattern, "Fred" }, "default message");
+        
         replay();
-
+        
         new Pattern("pattern=\\d+").renderContribution(writer, cycle, context, field);
-
+        
         verify();
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"default message\"},"
+                + "\"constraints\":{\"myfield\":[tapestry.form.validation.isValidPattern,\""
+                + pattern + "\"]}}",
+                json.toString());
     }
-
+    
     public void testRenderContributionCustomMessage()
     {
-        String pattern = new RegexpMatcher().getEscapedPatternString("\\d+");
-
+        String rawPattern = "\\d+";
+        String pattern = new RegexpMatcher().getEscapedPatternString(rawPattern);
+        
         IMarkupWriter writer = newWriter();
         IRequestCycle cycle = newCycle();
+        JSONObject json = new JSONObject();
+        
         FormComponentContributorContext context = newMock(FormComponentContributorContext.class);
-
-        context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
-
+        
         IFormComponent field = newField("Fred", "myfield");
-
+        
+        expect(context.getProfile()).andReturn(json);
+        
         trainFormatMessage(
                 context,
                 "custom",
                 ValidationStrings.REGEX_MISMATCH,
                 new Object[]
-                { "Fred" },
+                { rawPattern, "Fred" },
                 "custom\\message");
-
-        context
-                .addSubmitHandler("function(event) { Tapestry.validate_regex(event, 'myfield', '"
-                        + pattern + "', 'custom\\\\message'); }");
-
+        
         replay();
-
+        
         new Pattern("pattern=\\d+,message=custom").renderContribution(writer, cycle, context, field);
-
+        
         verify();
-
+        
+        assertEquals("{\"myfield\":{\"constraints\":\"custom\\\\message\"},"
+                + "\"constraints\":{\"myfield\":[tapestry.form.validation.isValidPattern,\""
+                + pattern + "\"]}}",
+                json.toString());
     }
-
+    
 }