Commits

unkyaku  committed 63e30f4

Support trim parameter in string validators.
Support minLength parameter in StringLengthFieldValidator.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@282e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits d5e9c01

Comments (0)

Files changed (5)

File src/java/com/opensymphony/xwork/validator/validators/RequiredStringValidator.java

  */
 package com.opensymphony.xwork.validator.validators;
 
-import com.opensymphony.util.TextUtils;
-
 import com.opensymphony.xwork.validator.ValidationException;
 
-import java.util.Map;
-
 
 /**
- * RequiredStringValidator checks that a String field is non-null and has a length > 0 (ie it isn't "")
- *
- * @author $Author$
- * @version $Revision$
+ * RequiredStringValidator checks that a String field is non-null and has a length > 0
+ * (i.e. it isn't "").  The "trim" parameter determines whether it will {@link String#trim() trim}
+ * the String before performing the length check.  If unspecified, the String will be trimmed.
  */
 public class RequiredStringValidator extends FieldValidatorSupport {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private boolean m_doTrim = true;
+
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void setTrim(boolean trim) {
+        m_doTrim = trim;
+    }
+
+    public boolean getTrim() {
+        return m_doTrim;
+    }
+
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         Object value = this.getFieldValue(fieldName, object);
 
-        if (!(value instanceof String) || !TextUtils.stringSet((String) value)) {
+        if (!(value instanceof String)) {
             addFieldError(fieldName, object);
+        } else {
+            String s = (String) value;
+
+            if (m_doTrim) {
+                s = s.trim();
+            }
+
+            if (s.length() == 0) {
+                addFieldError(fieldName, object);
+            }
         }
     }
 }

File src/java/com/opensymphony/xwork/validator/validators/StringLengthFieldValidator.java

 
 
 /**
- * StringLengthFieldValidator
+ * StringLengthFieldValidator checks that a String field is of a certain length.  If the "minLength"
+ * parameter is specified, it will make sure that the String has at least that many characters.  If
+ * the "maxLength" parameter is specified, it will make sure that the String has at most that many
+ * characters.  The "trim" parameter determines whether it will {@link String#trim() trim} the
+ * String before performing the length check.  If unspecified, the String will be trimmed.
+ *
+ *
  * @author Jason Carreira
- * Date: May 20, 2004 9:02:16 PM
+ * @author Mark Woon
  */
 public class StringLengthFieldValidator extends FieldValidatorSupport {
     //~ Instance fields ////////////////////////////////////////////////////////
 
-    private int maxLength = 0;
+    private boolean m_doTrim = true;
+    private int m_maxLength = -1;
+    private int m_minLength = -1;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public void setMaxLength(int maxLength) {
-        this.maxLength = maxLength;
+        this.m_maxLength = maxLength;
     }
 
     public int getMaxLength() {
-        return maxLength;
+        return m_maxLength;
+    }
+
+    public void setMinLength(int minLength) {
+        this.m_minLength = minLength;
+    }
+
+    public int getMinLength() {
+        return m_minLength;
+    }
+
+    public void setTrim(boolean trim) {
+        m_doTrim = trim;
+    }
+
+    public boolean getTrim() {
+        return m_doTrim;
     }
 
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         String val = (String) getFieldValue(fieldName, object);
 
-        if (val.length() > maxLength) {
+        if (val == null) {
             addFieldError(fieldName, object);
+        } else {
+            if (m_doTrim) {
+                val = val.trim();
+            }
+
+            if ((m_minLength > -1) && (val.length() < m_minLength)) {
+                addFieldError(fieldName, object);
+            } else if ((m_maxLength > -1) && (val.length() > m_maxLength)) {
+                addFieldError(fieldName, object);
+            }
         }
     }
 }

File src/test/com/opensymphony/xwork/test/Equidae-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="horse">
+        <field-validator type="requiredstring">
+            <param name="trim">false</param>
+            <message>noTrim</message>
+        </field-validator>
+        <field-validator type="requiredstring">
+            <param name="trim">true</param>
+            <message>trim</message>
+        </field-validator>
+    </field>
+
+    <field name="cow">
+        <field-validator type="stringlength">
+            <param name="trim">false</param>
+            <param name="minLength">5</param>
+            <message>noTrim-min5</message>
+        </field-validator>
+        <field-validator type="stringlength">
+            <param name="trim">false</param>
+            <param name="minLength">5</param>
+            <param name="maxLength">10</param>
+            <message>noTrim-min5-max10</message>
+        </field-validator>
+        <field-validator type="stringlength">
+            <param name="trim">false</param>
+            <param name="maxLength">10</param>
+            <message>noTrim-max10</message>
+        </field-validator>
+    </field>
+
+    <field name="donkey">
+        <field-validator type="stringlength">
+            <param name="trim">true</param>
+            <param name="minLength">5</param>
+            <message>trim-min5</message>
+        </field-validator>
+        <field-validator type="stringlength">
+            <param name="trim">true</param>
+            <param name="minLength">5</param>
+            <param name="maxLength">10</param>
+            <message>trim-min5-max10</message>
+        </field-validator>
+        <field-validator type="stringlength">
+            <param name="trim">true</param>
+            <param name="maxLength">10</param>
+            <message>trim-max10</message>
+        </field-validator>
+    </field>
+
+</validators>

File src/test/com/opensymphony/xwork/test/Equidae.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.test;
+
+
+/**
+ * @author Mark Woon
+ */
+public class Equidae {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private String m_cow;
+    private String m_donkey;
+    private String m_horse;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setCow(String cow) {
+        m_cow = cow;
+    }
+
+    public String getCow() {
+        return m_cow;
+    }
+
+    public void setDonkey(String donkey) {
+        m_donkey = donkey;
+    }
+
+    public String getDonkey() {
+        return m_donkey;
+    }
+
+    public void setHorse(String horse) {
+        m_horse = horse;
+    }
+
+    public String getHorse() {
+        return m_horse;
+    }
+}

File src/test/com/opensymphony/xwork/validator/StringValidatorTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.validator;
+
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.ValidationAwareSupport;
+import com.opensymphony.xwork.config.ConfigurationManager;
+import com.opensymphony.xwork.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork.test.Equidae;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import junit.framework.TestCase;
+
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * @author Mark Woon
+ */
+public class StringValidatorTest extends TestCase {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void testRequiredString() {
+        try {
+            Equidae equidae = new Equidae();
+
+            // everything should fail
+            equidae.setHorse("");
+            ActionContext.getContext().getValueStack().push(equidae);
+
+            DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+
+            assertTrue(context.hasFieldErrors());
+
+            Map fieldErrors = context.getFieldErrors();
+            assertTrue(fieldErrors.containsKey("horse"));
+            assertEquals(2, ((List) fieldErrors.get("horse")).size());
+
+            // trim = false should fail
+            equidae.setHorse("  ");
+            ActionContext.getContext().getValueStack().push(equidae);
+            context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+
+            assertTrue(context.hasFieldErrors());
+            fieldErrors = context.getFieldErrors();
+            assertTrue(fieldErrors.containsKey("horse"));
+
+            List errors = (List) fieldErrors.get("horse");
+            assertEquals(1, errors.size());
+            assertEquals("trim", (String) errors.get(0));
+        } catch (Exception ex) {
+            fail(ex.getMessage());
+        }
+    }
+
+    public void testStringLength() {
+        try {
+            Equidae equidae = new Equidae();
+
+            equidae.setCow("asdf");
+            equidae.setDonkey("asdf");
+            ActionContext.getContext().getValueStack().push(equidae);
+
+            DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+            assertTrue(context.hasFieldErrors());
+
+            Map fieldErrors = context.getFieldErrors();
+
+            // cow
+            assertTrue(fieldErrors.containsKey("cow"));
+
+            List errors = (List) fieldErrors.get("cow");
+            assertEquals(2, errors.size());
+            assertEquals("noTrim-min5", errors.get(0));
+            assertEquals("noTrim-min5-max10", errors.get(1));
+
+            // donkey
+            assertTrue(fieldErrors.containsKey("donkey"));
+            errors = (List) fieldErrors.get("donkey");
+            assertEquals(2, errors.size());
+            assertEquals("trim-min5", errors.get(0));
+            assertEquals("trim-min5-max10", errors.get(1));
+
+            equidae.setCow("asdf  ");
+            equidae.setDonkey("asdf  ");
+            ActionContext.getContext().getValueStack().push(equidae);
+            context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+            assertTrue(context.hasFieldErrors());
+
+            fieldErrors = context.getFieldErrors();
+
+            // cow
+            assertFalse(fieldErrors.containsKey("cow"));
+
+            // donkey
+            assertTrue(fieldErrors.containsKey("donkey"));
+            errors = (List) fieldErrors.get("donkey");
+            assertEquals(2, errors.size());
+            assertEquals("trim-min5", errors.get(0));
+            assertEquals("trim-min5-max10", errors.get(1));
+
+            equidae.setCow("asdfasdf");
+            equidae.setDonkey("asdfasdf");
+            ActionContext.getContext().getValueStack().push(equidae);
+            context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+            assertTrue(context.hasFieldErrors());
+
+            fieldErrors = context.getFieldErrors();
+
+            // cow
+            assertFalse(fieldErrors.containsKey("cow"));
+
+            // donkey
+            assertFalse(fieldErrors.containsKey("donkey"));
+
+            equidae.setCow("asdfasdf   ");
+            equidae.setDonkey("asdfasdf   ");
+            ActionContext.getContext().getValueStack().push(equidae);
+            context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+            assertTrue(context.hasFieldErrors());
+
+            fieldErrors = context.getFieldErrors();
+
+            // cow
+            assertTrue(fieldErrors.containsKey("cow"));
+            errors = (List) fieldErrors.get("cow");
+            assertEquals(2, errors.size());
+            assertEquals("noTrim-min5-max10", errors.get(0));
+            assertEquals("noTrim-max10", errors.get(1));
+
+            // donkey
+            assertFalse(fieldErrors.containsKey("donkey"));
+
+            equidae.setCow("asdfasdfasdf");
+            equidae.setDonkey("asdfasdfasdf");
+            ActionContext.getContext().getValueStack().push(equidae);
+            context = new DelegatingValidatorContext(new ValidationAwareSupport());
+            ActionValidatorManager.validate(equidae, null, context);
+            assertTrue(context.hasFieldErrors());
+
+            fieldErrors = context.getFieldErrors();
+
+            // cow
+            assertTrue(fieldErrors.containsKey("cow"));
+            errors = (List) fieldErrors.get("cow");
+            assertEquals(2, errors.size());
+            assertEquals("noTrim-min5-max10", errors.get(0));
+            assertEquals("noTrim-max10", errors.get(1));
+
+            // donkey
+            assertTrue(fieldErrors.containsKey("donkey"));
+            errors = (List) fieldErrors.get("donkey");
+            assertEquals(2, errors.size());
+            assertEquals("trim-min5-max10", errors.get(0));
+            assertEquals("trim-max10", errors.get(1));
+        } catch (Exception ex) {
+            fail(ex.getMessage());
+        }
+    }
+
+    protected void setUp() throws Exception {
+        OgnlValueStack stack = new OgnlValueStack();
+        ActionContext.setContext(new ActionContext(stack.getContext()));
+
+        ConfigurationManager.clearConfigurationProviders();
+        ConfigurationManager.addConfigurationProvider(new MockConfigurationProvider());
+        ConfigurationManager.getConfiguration().reload();
+    }
+}