Commits

Anonymous committed 86fe991

Adding a StringLengthValidator and fixing a bug with the conversion to Class objects

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

  • Participants
  • Parent commits 01eb759

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/util/XWorkBasicConverter.java

 import com.opensymphony.util.TextUtils;
 
 import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.XworkException;
 
 import ognl.DefaultTypeConverter;
 import ognl.Ognl;
 
 import java.text.DateFormat;
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
 
 import java.util.*;
 
             try {
                 clazz = Class.forName((String) value);
             } catch (ClassNotFoundException e) {
+                throw new XworkException(e.getLocalizedMessage(), e);
             }
         }
 
             try {
                 result = df.parse(sa);
             } catch (ParseException e) {
-                throw new RuntimeException("Could not parse date");
+                throw new XworkException("Could not parse date", e);
             }
         } else if (Date.class.isAssignableFrom(value.getClass())) {
             result = (Date) value;

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

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.validator.validators;
+
+import com.opensymphony.xwork.validator.ValidationException;
+
+
+/**
+ * StringLengthFieldValidator
+ * @author Jason Carreira
+ * Date: May 20, 2004 9:02:16 PM
+ */
+public class StringLengthFieldValidator extends FieldValidatorSupport {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private int maxLength = 0;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setMaxLength(int maxLength) {
+        this.maxLength = maxLength;
+    }
+
+    public int getMaxLength() {
+        return maxLength;
+    }
+
+    public void validate(Object object) throws ValidationException {
+        String fieldName = getFieldName();
+        String val = (String) getFieldValue(fieldName, object);
+
+        if (val.length() > maxLength) {
+            addFieldError(fieldName, object);
+        }
+    }
+}

File src/test/validators.xml

     <validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>
     <validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>
     <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>
+    <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
 </validators>