Author: lukaszlenart
Date: Thu Jan  3 11:14:26 2013
New Revision: 1428282

URL: http://svn.apache.org/viewvc?rev=1428282&view=rev
Log:
WW-3893 adds support to specify minLength, maxLength and trim params as 
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=1428282&r1=1428281&r2=1428282&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
 Thu Jan  3 11:14:26 2013
@@ -31,10 +31,11 @@ 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.</li>
- *    <li>minLength - The min length of the field value. Default ignore.</li>
- *    <li>trim - Trim the field value before evaluating its min/max length. 
Default true</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>
  * </ul>
+ * WARNING! Do not use ${minLength}, ${maxLength} and ${trim} as an expression 
as this will turn into infinitive loop!
  * <!-- END SNIPPET: parameters -->
  * 
  * 
@@ -59,6 +60,16 @@ import com.opensymphony.xwork2.validator
  *                     &lt;message&gt;Your purchase code needs to be 10 
characters long&lt;/message&gt;
  *                &lt;/field-validator&gt;
  *            &lt;/field&gt;
+ *
+ *            &lt;!-- Field Validator Syntax with expression --&gt;
+ *            &lt;field name="myPurchaseCode"&gt;
+ *                &lt;field-validator type="stringlength"&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;message&gt;Your purchase code needs to be 10 
characters long&lt;/message&gt;
+ *                &lt;/field-validator&gt;
+ *            &lt;/field&gt;
  *      &lt;/validators&gt;
  * <!-- END SNIPPET: example -->
  * </pre>
@@ -71,33 +82,44 @@ import com.opensymphony.xwork2.validator
  */
 public class StringLengthFieldValidator extends FieldValidatorSupport {
 
-    private boolean doTrim = true;
+    private boolean trim = true;
     private int maxLength = -1;
     private int minLength = -1;
 
-
-    public void setMaxLength(int maxLength) {
-        this.maxLength = maxLength;
+    public void setMaxLength(String maxLength) {
+        if (parse) {
+            this.maxLength = (Integer) parse(maxLength, Integer.class);
+        } else {
+            this.maxLength = Integer.valueOf(maxLength);
+        }
     }
 
     public int getMaxLength() {
         return maxLength;
     }
 
-    public void setMinLength(int minLength) {
-        this.minLength = minLength;
+    public void setMinLength(String minLength) {
+        if (parse) {
+            this.minLength = (Integer) parse(minLength, Integer.class);
+        } else {
+            this.minLength = Integer.parseInt(minLength);
+        }
     }
 
     public int getMinLength() {
         return minLength;
     }
 
-    public void setTrim(boolean trim) {
-        doTrim = trim;
+    public void setTrim(String trim) {
+        if (parse) {
+            this.trim = (Boolean) parse(trim, Boolean.class);
+        } else {
+            this.trim = Boolean.parseBoolean(trim);
+        }
     }
 
-    public boolean getTrim() {
-        return doTrim;
+    public boolean isTrim() {
+        return trim;
     }
 
     public void validate(Object object) throws ValidationException {
@@ -108,11 +130,11 @@ public class StringLengthFieldValidator 
             // use a required validator for these
             return;
         }
-        if (doTrim) {
+        if (trim) {
             val = val.trim();
-            if (val.length() <= 0) { 
-               // use a required validator
-               return;
+            if (val.length() <= 0) {
+                // use a required validator
+                return;
             }
         }
 
@@ -122,4 +144,5 @@ public class StringLengthFieldValidator 
             addFieldError(fieldName, object);
         }
     }
+
 }

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=1428282&r1=1428281&r2=1428282&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
 Thu Jan  3 11:14:26 2013
@@ -18,6 +18,7 @@ package com.opensymphony.xwork2.validato
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;
 import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.util.ValueStack;
 import com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator;
 
 /**
@@ -33,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(), "");
@@ -43,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);
@@ -53,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(), "   ");
@@ -63,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(), "          ");
@@ -74,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);
@@ -84,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");
@@ -94,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   ");
@@ -104,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(), "   ");
@@ -114,25 +115,58 @@ public class StringLengthFieldValidatorT
        public void testValidStringLengthTrim() throws Exception {
                action.setMyField("asd          ");
                
-               validator.setTrim(true);
+               validator.setTrim("true");
                validator.validate(action);
                
                assertEquals(action.getMyField(), "asd          ");
                assertFalse(action.hasFieldErrors());
        }
-       
-       
-       @Override
+
+    public void testMinLengthViaExpression() throws Exception {
+        assertEquals(2, validator.getMinLength());
+        action.setMinLengthValue(10);
+
+        validator.setParse(true);
+        validator.setMinLength("${minLengthValue}");
+
+        assertEquals(10, validator.getMinLength());
+    }
+
+    public void testMaxLengthViaExpression() throws Exception {
+        assertEquals(5, validator.getMaxLength());
+        action.setMaxLengthValue(100);
+
+        validator.setParse(true);
+        validator.setMaxLength("${maxLengthValue}");
+
+        assertEquals(100, validator.getMaxLength());
+    }
+
+    public void testTrimViaExpression() throws Exception {
+        assertTrue(validator.isTrim());
+        action.setTrimValue(false);
+
+        validator.setParse(true);
+        validator.setTrim("${trimValue}");
+
+        assertFalse(validator.isTrim());
+    }
+
+    @Override
        protected void setUp() throws Exception {
                super.setUp();
-               action = new InternalActionSupport();
+
+        action = new InternalActionSupport();
+        ValueStack valueStack = ActionContext.getContext().getValueStack();
+        valueStack.push(action);
+
                validator = new StringLengthFieldValidator();
                validator.setFieldName("myField");
                validator.setMessageKey("error");
                validator.setValidatorContext(new 
DelegatingValidatorContext(action));
-               validator.setMaxLength(5);
-               validator.setMinLength(2);
-        validator.setValueStack(ActionContext.getContext().getValueStack());
+               validator.setMaxLength("5");
+               validator.setMinLength("2");
+        validator.setValueStack(valueStack);
     }
        
        
@@ -146,9 +180,44 @@ public class StringLengthFieldValidatorT
        public static class InternalActionSupport extends ActionSupport {
 
                private static final long serialVersionUID = 1L;
-               
-               private String myField;
-               public String getMyField() { return this.myField; }
-               public void setMyField(String myField) { this.myField = 
myField; }
-       }
+
+        private String myField;
+        private boolean trimValue;
+        private int minLengthValue;
+        private int maxLengthValue;
+
+        public String getMyField() {
+            return this.myField;
+        }
+
+        public void setMyField(String myField) {
+            this.myField = myField;
+        }
+
+        public boolean isTrimValue() {
+            return trimValue;
+        }
+
+        public void setTrimValue(boolean trimValue) {
+            this.trimValue = trimValue;
+        }
+
+        public int getMinLengthValue() {
+            return minLengthValue;
+        }
+
+        public void setMinLengthValue(int minLengthValue) {
+            this.minLengthValue = minLengthValue;
+        }
+
+        public int getMaxLengthValue() {
+            return maxLengthValue;
+        }
+
+        public void setMaxLengthValue(int maxLengthValue) {
+            this.maxLengthValue = maxLengthValue;
+        }
+
+    }
+
 }


Reply via email to