You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by th...@apache.org on 2014/01/03 20:19:07 UTC

git commit: TAP5-1998. DateField does not validate dates properly. From now on, Tapestry will use non-lenient DateFields unless configured otherwise, through the SymbolConstants.LENIENT_DATE_FORMAT symbol or the DateField 'lenient' parameter.

Updated Branches:
  refs/heads/master 592989f4a -> 913c80b01


TAP5-1998. DateField does not validate dates properly. From now on,
Tapestry will use non-lenient DateFields unless configured otherwise,
through the SymbolConstants.LENIENT_DATE_FORMAT symbol or the DateField
'lenient' parameter.

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

Branch: refs/heads/master
Commit: 913c80b012c780f3de6e03c3471358e053217d87
Parents: 592989f
Author: Thiago H. de Paula Figueiredo <th...@apache.org>
Authored: Fri Jan 3 17:18:50 2014 -0200
Committer: Thiago H. de Paula Figueiredo <th...@apache.org>
Committed: Fri Jan 3 17:18:50 2014 -0200

----------------------------------------------------------------------
 .../org/apache/tapestry5/SymbolConstants.java   |  9 +++++-
 .../tapestry5/corelib/components/DateField.java | 31 +++++++++++++++++---
 .../tapestry5/modules/TapestryModule.java       | 10 +++++--
 tapestry-core/src/test/app1/DateFieldDemo.tml   |  7 +++++
 .../tapestry5/integration/app1/FormTests.java   | 25 +++++++++++++++-
 .../integration/app1/pages/DateFieldDemo.java   | 17 +++++++++--
 6 files changed, 89 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
index c101825..83e6f9c 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/SymbolConstants.java
@@ -1,4 +1,4 @@
-// Copyright 2008-2013 The Apache Software Foundation
+// Copyright 2008-2014 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.
@@ -474,5 +474,12 @@ public class SymbolConstants
      * @since 5.4
      */
     public static final String FORM_FIELD_CSS_CLASS = "tapestry.form-field-css-class";
+    
+    /**
+     * Defines whether {@link java.text.DateFormat} instances created by Tapestry should be
+     * lenient or not by default. The default value is <code>false</code>.
+     * @since 5.4
+     */
+    public static final String LENIENT_DATE_FORMAT = "tapestry.lenient-date-format";
 
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/DateField.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/DateField.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/DateField.java
index aebb4a6..286a845 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/DateField.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/DateField.java
@@ -1,4 +1,4 @@
-// Copyright 2007-2013 The Apache Software Foundation
+// Copyright 2007-2014 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.
@@ -22,6 +22,7 @@ import org.apache.tapestry5.annotations.RequestParameter;
 import org.apache.tapestry5.corelib.base.AbstractField;
 import org.apache.tapestry5.ioc.Messages;
 import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.annotations.Symbol;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 import org.apache.tapestry5.json.JSONObject;
 import org.apache.tapestry5.services.ComponentDefaultProvider;
@@ -68,6 +69,18 @@ public class DateField extends AbstractField
      */
     @Parameter(required = true, allowNull = false, defaultPrefix = BindingConstants.LITERAL)
     private DateFormat format;
+    
+    /**
+     * When the <code>format</code> parameter isn't used, this parameter defines whether the
+     * <code>DateFormat</code> created by this component will be lenient or not.
+     * The default value of this parameter is the value of the {@link SymbolConstants#LENIENT_DATE_FORMAT}
+     * symbol.
+     * @see DateFormat#setLenient(boolean)
+     * @see SymbolConstants#LENIENT_DATE_FORMAT
+     * @since 5.4
+     */
+    @Parameter(principal = true)
+    private boolean lenient;
 
     /**
      * If true, then the text field will be hidden, and only the icon for the date picker will be visible. The default
@@ -83,7 +96,7 @@ public class DateField extends AbstractField
     @Parameter(defaultPrefix = BindingConstants.VALIDATE)
     @SuppressWarnings("unchecked")
     private FieldValidator<Object> validate;
-
+    
     /**
      * Icon used for the date field trigger button. This was used in Tapestry 5.3 and earlier and is now ignored.
      *
@@ -106,6 +119,10 @@ public class DateField extends AbstractField
 
     @Inject
     private DeprecationWarning deprecationWarning;
+    
+    @Inject
+    @Symbol(SymbolConstants.LENIENT_DATE_FORMAT)
+    private boolean lenientDateFormatSymbolValue;
 
     private static final String RESULT = "result";
 
@@ -128,8 +145,10 @@ public class DateField extends AbstractField
             String pattern = simpleDateFormat.toPattern();
 
             String revised = pattern.replaceAll("([^y])yy$", "$1yyyy");
-
-            return new SimpleDateFormat(revised);
+            
+            final SimpleDateFormat revisedDateFormat = new SimpleDateFormat(revised);
+            revisedDateFormat.setLenient(lenient);
+            return revisedDateFormat;
         }
 
         return shortDateFormat;
@@ -142,6 +161,10 @@ public class DateField extends AbstractField
     {
         return defaultProvider.defaultValidatorBinding("value", resources);
     }
+    
+    final boolean defaultLenient() {
+        return lenientDateFormatSymbolValue;
+    }
 
     /**
      * Ajax event handler, used when initiating the popup. The client sends the input value form the field to the server

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java b/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
index e7c7f87..4393236 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/modules/TapestryModule.java
@@ -1,4 +1,4 @@
-// Copyright 2006-2013 The Apache Software Foundation
+// Copyright 2006-2014 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.
@@ -1038,7 +1038,10 @@ public final class TapestryModule
         {
             public DateFormat coerce(String input)
             {
-                return new SimpleDateFormat(input, threadLocale.getLocale());
+                final SimpleDateFormat dateFormat = new SimpleDateFormat(input, threadLocale.getLocale());
+                final String lenient = objectLocator.getService(SymbolSource.class).valueForSymbol(SymbolConstants.LENIENT_DATE_FORMAT);
+                dateFormat.setLenient(Boolean.parseBoolean(lenient));
+                return dateFormat;
             }
         }));
 
@@ -2117,6 +2120,9 @@ public final class TapestryModule
         configuration.add(SymbolConstants.FORM_GROUP_FORM_FIELD_WRAPPER_ELEMENT_NAME, "");
         configuration.add(SymbolConstants.FORM_GROUP_FORM_FIELD_WRAPPER_ELEMENT_CSS_CLASS, "");
         configuration.add(SymbolConstants.FORM_FIELD_CSS_CLASS, "form-control");
+        
+        // TAP5-1998
+        configuration.add(SymbolConstants.LENIENT_DATE_FORMAT, "false");
 
     }
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/test/app1/DateFieldDemo.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/app1/DateFieldDemo.tml b/tapestry-core/src/test/app1/DateFieldDemo.tml
index ff21040..671383d 100644
--- a/tapestry-core/src/test/app1/DateFieldDemo.tml
+++ b/tapestry-core/src/test/app1/DateFieldDemo.tml
@@ -16,6 +16,11 @@
         <t:datefield t:id="asteroidImpact"/>
     </div>
 
+    <div class="field-group x-lenient">
+        <t:label for="lenient"/>
+        <t:datefield t:id="lenient" lenient="true"/>
+    </div>
+
     <input type="submit" value="Go" class="btn btn-primary"/>
 
 </t:form>
@@ -39,6 +44,8 @@
     <dd id="impact-output">
         <t:output value="asteroidImpact" format="dateFormat"/>
     </dd>
+    <dt>Is coerced string to DateFormat lenient?</dt>
+    <dd id="coercedStringToDateFormatLenient">${coercedStringToDateFormatLenient}</dd>
 </dl>
 
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
index 5a7732e..37876a6 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/FormTests.java
@@ -1,4 +1,4 @@
-// Copyright 2009-2013 The Apache Software Foundation
+// Copyright 2009-2014 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.
@@ -209,6 +209,29 @@ public class FormTests extends App1TestCase
 
         assertText("//A[@class='topLabel']", "1966 d\u00e9cembre");
     }
+    
+    // TAP5-2197
+    @Test
+    public void datefield_leniency()
+    {
+        openLinks("DateField Demo", "Reset Page State", "english");
+
+        type("asteroidImpact", "00/00/0000");
+        type("lenient", "00/00/0000");
+        
+        clickAndWait(SUBMIT);
+        
+        // By default, DateField is not lenient anymore
+        assertText("css=div.x-impact p.help-block", "Date value '00/00/0000' is not parseable.");
+        
+        // But this one is configured as such by setting the "lenient" parameter to true.
+        assertFalse(isElementPresent("css=div.x-lenient p.help-block"));
+
+        // Check whether a String coerced to a DateFormat results in a lenient or not instance
+        // according to the SymbolConstants.LENIENT_DATE_FORMAT symbol (default false)
+        assertText("coercedStringToDateFormatLenient", "false");
+        
+    }
 
     // TAP5-1057
     @Test

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/913c80b0/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/DateFieldDemo.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/DateFieldDemo.java b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/DateFieldDemo.java
index 0b628b2..f17e20e 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/DateFieldDemo.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/DateFieldDemo.java
@@ -1,4 +1,4 @@
-// Copyright 2007, 2008, 2013 The Apache Software Foundation
+// Copyright 2007, 2008, 2013, 2014 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.
@@ -18,6 +18,7 @@ import org.apache.tapestry5.annotations.Persist;
 import org.apache.tapestry5.annotations.Property;
 import org.apache.tapestry5.beaneditor.Validate;
 import org.apache.tapestry5.ioc.annotations.Inject;
+import org.apache.tapestry5.ioc.services.TypeCoercer;
 import org.apache.tapestry5.services.PersistentLocale;
 
 import java.text.DateFormat;
@@ -37,10 +38,17 @@ public class DateFieldDemo
     @Validate("required")
     private Date asteroidImpact;
 
+    @Persist
+    @Property
+    @Validate("required")
+    private Date lenient;
+
     @Inject
     private PersistentLocale persistentLocale;
 
-
+    @Inject
+    private TypeCoercer typeCoercer;
+    
     public DateFormat getDateFormat()
     {
         return new SimpleDateFormat("MM/dd/yyyy");
@@ -62,4 +70,9 @@ public class DateFieldDemo
     {
         persistentLocale.set(Locale.FRENCH);
     }
+    
+    public boolean isCoercedStringToDateFormatLenient()
+    {
+        return typeCoercer.coerce("dd/MM/yyyy", java.text.DateFormat.class).isLenient();
+    }
 }