struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1432595 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
Date Sun, 13 Jan 2013 12:08:20 GMT
Author: lukaszlenart
Date: Sun Jan 13 12:08:19 2013
New Revision: 1432595

URL: http://svn.apache.org/viewvc?rev=1432595&view=rev
Log:
WW-3893 adds optional to params to define minLength, maxLength and trim params as an OGNL
expression

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java?rev=1432595&r1=1432594&r2=1432595&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/StringLengthFieldValidator.java
Sun Jan 13 12:08:19 2013
@@ -31,14 +31,17 @@ import com.opensymphony.xwork2.validator
  * <!-- START SNIPPET: parameters -->
  * <ul>
  *    <li>fieldName - The field name this validator is validating. Required if using
Plain-Validator Syntax otherwise not required</li>
- *    <li>maxLength - The max length of the field value. Default ignore. Can be specified
as OGNL expression when parse is set to true.</li>
- *    <li>minLength - The min length of the field value. Default ignore. Can be specified
as OGNL expression when parse is set to true.</li>
- *    <li>trim - Trim the field value before evaluating its min/max length. Default
true. Can be specified as OGNL expression when parse is set to true.</li>
+ *    <li>maxLength - Integer. The max length of the field value. Default ignore.</li>
+ *    <li>minLength - Integer. The min length of the field value. Default ignore.</li>
+ *    <li>trim - (Optional) Boolean, default true. Trim the field value before evaluating
its min/max length. Default true.</li>
+ *    <li>maxLengthExpression - (Optional) String. Defines the max length param as
an OGNL expression</li>
+ *    <li>minLengthExpression - (Optional) String. Defines the min length param as
an OGNL expression</li>
+ *    <li>trimExpression - (Optional) String. Defines th trim param as an OGNL expression</li>
  * </ul>
  * <!-- END SNIPPET: parameters -->
  * 
  * <!-- START SNIPPET: parameters-warning -->
- * Do not use ${minLength}, ${maxLength} and ${trim} as an expression as this will turn into
infinitive loop!
+ * Do not use ${minLengthExpression}, ${maxLengthExpression} and ${trimExpression} as an
expression as this will turn into infinitive loop!
  * <!-- END SNIPPET: parameters-warning -->
  *
  * <pre>
@@ -66,10 +69,9 @@ import com.opensymphony.xwork2.validator
  *     &lt;!-- Field Validator Syntax with expression --&gt;
  *     &lt;field name="myPurchaseCode"&gt;
  *         &lt;field-validator type="stringlength"&gt;
- *              &lt;param name="parse"&gt;true&lt;/param&gt;
- *              &lt;param name="minLength"&gt;${minLengthValue}&lt;/param&gt;
&lt;!-- will be evaluated as: Integer getMinLengthValue() --&gt;
- *              &lt;param name="maxLength"&gt;${maxLengthValue}&lt;/param&gt;
&lt;!-- will be evaluated as: Integer getMaxLengthValue() --&gt;
- *              &lt;param name="trim"&gt;${trimValue}&lt;/param&gt; &lt;!--
will be evaluated as: boolean getTrimValue() --&gt;
+ *              &lt;param name="minLengthExpression"&gt;${minLengthValue}&lt;/param&gt;
&lt;!-- will be evaluated as: Integer getMinLengthValue() --&gt;
+ *              &lt;param name="maxLengthExpression"&gt;${maxLengthValue}&lt;/param&gt;
&lt;!-- will be evaluated as: Integer getMaxLengthValue() --&gt;
+ *              &lt;param name="trimExpression"&gt;${trimValue}&lt;/param&gt;
&lt;!-- will be evaluated as: boolean getTrimValue() --&gt;
  *              &lt;message&gt;Your purchase code needs to be 10 characters long&lt;/message&gt;
  *         &lt;/field-validator&gt;
  *     &lt;/field&gt;
@@ -89,36 +91,36 @@ public class StringLengthFieldValidator 
     private int maxLength = -1;
     private int minLength = -1;
 
-    public void setMaxLength(String maxLength) {
-        if (parse) {
-            this.maxLength = (Integer) parse(maxLength, Integer.class);
-        } else {
-            this.maxLength = Integer.valueOf(maxLength);
-        }
+    public void setMaxLength(int maxLength) {
+        this.maxLength = maxLength;
+    }
+
+    public void setMaxLengthExpression(String maxLengthExpression) {
+        this.maxLength = (Integer) parse(maxLengthExpression, Integer.class);
     }
 
     public int getMaxLength() {
         return maxLength;
     }
 
-    public void setMinLength(String minLength) {
-        if (parse) {
-            this.minLength = (Integer) parse(minLength, Integer.class);
-        } else {
-            this.minLength = Integer.parseInt(minLength);
-        }
+    public void setMinLength(int minLength) {
+        this.minLength = minLength;
+    }
+
+    public void setMinLengthExpression(String minLengthExpression) {
+        this.minLength = (Integer) parse(minLengthExpression, Integer.class);
     }
 
     public int getMinLength() {
         return minLength;
     }
 
-    public void setTrim(String trim) {
-        if (parse) {
-            this.trim = (Boolean) parse(trim, Boolean.class);
-        } else {
-            this.trim = Boolean.parseBoolean(trim);
-        }
+    public void setTrim(boolean trim) {
+        this.trim = trim;
+    }
+
+    public void setTrimExpression(String trimExpression) {
+        this.trim = (Boolean) parse(trimExpression, Boolean.class);
     }
 
     public boolean isTrim() {

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java?rev=1432595&r1=1432594&r2=1432595&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/StringLengthFieldValidatorTest.java
Sun Jan 13 12:08:19 2013
@@ -34,7 +34,7 @@ public class StringLengthFieldValidatorT
 	public void testStringLengthEmptyNoTrim1() throws Exception {
 		action.setMyField("");
 		
-		validator.setTrim("false");
+		validator.setTrim(false);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "");
@@ -44,7 +44,7 @@ public class StringLengthFieldValidatorT
 	public void testStringLengthNullNoTrim() throws Exception {
 		action.setMyField(null);
 
-		validator.setTrim("false");
+		validator.setTrim(false);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), null);
@@ -54,7 +54,7 @@ public class StringLengthFieldValidatorT
 	public void testStringLengthEmptyTrim1() throws Exception {
 		action.setMyField("   ");
 		
-		validator.setTrim("true");
+		validator.setTrim(true);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "   ");
@@ -64,7 +64,7 @@ public class StringLengthFieldValidatorT
 	public void testStringLengthEmptyNoTrim2() throws Exception {
 		action.setMyField("          ");
 		
-		validator.setTrim("false");
+		validator.setTrim(false);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "          ");
@@ -75,7 +75,7 @@ public class StringLengthFieldValidatorT
 	public void testStringLengthNullTrim() throws Exception {
 		action.setMyField(null);
 
-		validator.setTrim("true");
+		validator.setTrim(true);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), null);
@@ -85,7 +85,7 @@ public class StringLengthFieldValidatorT
 	public void testInvalidStringLengthNoTrim() throws Exception {
 		action.setMyField("abcdefghijklmn");
 		
-		validator.setTrim("false");
+		validator.setTrim(false);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "abcdefghijklmn");
@@ -95,7 +95,7 @@ public class StringLengthFieldValidatorT
 	public void testInvalidStringLengthTrim() throws Exception {
 		action.setMyField("abcdefghijklmn   ");
 		
-		validator.setTrim("true");
+		validator.setTrim(true);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "abcdefghijklmn   ");
@@ -105,7 +105,7 @@ public class StringLengthFieldValidatorT
 	public void testValidStringLengthNoTrim() throws Exception {
 		action.setMyField("   ");
 		
-		validator.setTrim("false");
+		validator.setTrim(false);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "   ");
@@ -115,7 +115,7 @@ public class StringLengthFieldValidatorT
 	public void testValidStringLengthTrim() throws Exception {
 		action.setMyField("asd          ");
 		
-		validator.setTrim("true");
+		validator.setTrim(true);
 		validator.validate(action);
 		
 		assertEquals(action.getMyField(), "asd          ");
@@ -126,8 +126,7 @@ public class StringLengthFieldValidatorT
         assertEquals(2, validator.getMinLength());
         action.setMinLengthValue(10);
 
-        validator.setParse(true);
-        validator.setMinLength("${minLengthValue}");
+        validator.setMinLengthExpression("${minLengthValue}");
 
         assertEquals(10, validator.getMinLength());
     }
@@ -136,8 +135,7 @@ public class StringLengthFieldValidatorT
         assertEquals(5, validator.getMaxLength());
         action.setMaxLengthValue(100);
 
-        validator.setParse(true);
-        validator.setMaxLength("${maxLengthValue}");
+        validator.setMaxLengthExpression("${maxLengthValue}");
 
         assertEquals(100, validator.getMaxLength());
     }
@@ -146,8 +144,7 @@ public class StringLengthFieldValidatorT
         assertTrue(validator.isTrim());
         action.setTrimValue(false);
 
-        validator.setParse(true);
-        validator.setTrim("${trimValue}");
+        validator.setTrimExpression("${trimValue}");
 
         assertFalse(validator.isTrim());
     }
@@ -164,8 +161,8 @@ public class StringLengthFieldValidatorT
 		validator.setFieldName("myField");
 		validator.setMessageKey("error");
 		validator.setValidatorContext(new DelegatingValidatorContext(action));
-		validator.setMaxLength("5");
-		validator.setMinLength("2");
+		validator.setMaxLength(5);
+		validator.setMinLength(2);
         validator.setValueStack(valueStack);
     }
 	



Mime
View raw message